using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Common;
using GlassLib;
using WinControls.ListView;
using WindowsHelper;
using BrendanGrant.Helpers.FileAssociation;
using Timer=System.Windows.Forms.Timer;
using Microsoft.Win32;
using IWshRuntimeLibrary;
using File=System.IO.File;
using ThreadState=System.Threading.ThreadState;
using Ini;
using System.Security;
using VistaControls.TaskDialog;



namespace GlobalHooksTest
{
	public class frmSbarDevelopper : Form
    {
        public static string GetExeDir(string ExePath)
        {
            return Path.GetDirectoryName(ExePath);
        }
        private IContainer components;
        public static IniFile SettingsIni = null;
        private NotifyIcon icSystray;
        List<Process> proclst = new List<Process>();
	    private   IntPtr FHandle = IntPtr.Zero;
	    private IntPtr FullScreenApp = IntPtr.Zero;
	    private Timer TNew = null;
        public static bool WorkAreaReserve = false;
        public static bool Taskbarhide = true;
        public static  string ExePath = GetExeDir(Application.ExecutablePath);
        public static string SkinDir = "Aero";
        private static int TaskXPos = 0;
        private static int TaskYPos = 0;
        public static bool ThuDisable = false;
	    public int PX = 0;
	    private readonly SysImageList lst = new SysImageList(SysImageListSize.largeIcons);
        private readonly SysImageList lsts = new SysImageList(SysImageListSize.smallIcons);
        public static OperatingSystemVersion sv = new OperatingSystemVersion();
        public static bool IsLeaved;
	    public   Form ThumbForm = null;
	    private Form RKF = null;
	    private   int icControlPanel = 0;
	    private   int icPrinterandfaxes = 0;
	    private    int icDocumentsandsettings = 0;
	    private   int icGames = 0;
        public   Bitmap StartBarBackgroubd = new Bitmap(ExePath + "\\Skins\\" + SkinDir + "\\Startbar.png");
        private   IniFile Skin;
        public static List<string> ExclusionList = new List<string>();
	    private bool IsPushMinimize = false;
        public   string ButtonBmpS = "";
        public   string ButtonBmpS2 = "";
        public   string ButtonBmpS3 = "";
        public   string ButtonQLH = "";
	    public   string ButtonShowDesktopS = "";
        public   string ButtonShowDesktopAS = "";
        public   string ButtonPressedBmpS = "";
        public   string ButtonPressedBmpS2 = "";
        public   string ButtonPressedBmpS3 = "";
        public   string ButtonHoverBmpS = "";
        public   string ButtonHoverBmpS2 = "";
        public   string ButtonHoverBmpS3 = "";
        public   Bitmap ButtonFlash = null;
        public   Bitmap ButtonFlash2 = null;
        public   Bitmap ButtonFlash3 = null;
        public   Bitmap PopUpFormBgr = null;
        public   Bitmap PopUpButtonBgr = null;
        public   Bitmap PopUpButtonBgrA = null;
        public   Bitmap PopUpButtonBgrAF = null;
        public   string TransbgrS = "";
        public string HoverBgrThu = "";
        public   string TransbgrAS = "";
        public   string CountBadgeS = "";
        public  static  string FlyoutbgrWindowCloseS = "";
        public   string TransS = "";
        public   string FlyoutbgrS = "";
        public   string Menu1S = "";
        public   string Menu1lkuS = "";
        public   string Menu1lkdS = "";
        public   string Menu1rkuS = "";
        public   string Menu1rkdS = "";
        public   string Menu1lmS = "";
        public   string Menu1rmS = "";
        public   string Menu1umS = "";
        public   string Menu1dmS = "";
        public   string FlashbgrS = "";
	    public   WindowsAPI.RECT Desktop;
	    private   PerPixelAlphaForm PopUP;
	    private int InitialareaX;
        private int InitialAreaY;
	    private Form PForm;
	    public   PerPixelAlphaForm RightClickForm = null;
        private readonly WinControls.ListView.TreeListView treeListView1 = new TreeListView();
        private   PerPixelAlphaForm T;
	    private PerPixelAlphaForm FO;
	    readonly List<Timer> Timers = new List<Timer>();
        private static readonly IntPtr hDesktop = WindowsAPI.GetDesktopWindow();
        private static readonly IntPtr hTray = WindowsAPI.FindWindowEx(hDesktop, IntPtr.Zero, "Shell_TrayWnd", null);
        public static readonly IntPtr hReBar = WindowsAPI.FindWindowEx(hTray, IntPtr.Zero, "ReBarWindow32", null);
        private static int CountBadgeBool = 0;
        public static readonly IntPtr hQuickLaunch = WindowsAPI.FindWindowEx(hReBar, IntPtr.Zero, "ToolbarWindow32",
	                                                                         "Quick Launch");
        public static readonly IntPtr hTask = WindowsAPI.FindWindowEx(hReBar, IntPtr.Zero, "MSTaskSwWClass", null);
        private static readonly IntPtr hToolbar = WindowsAPI.FindWindowEx(hTask, IntPtr.Zero, "ToolbarWindow32", null);
        private static readonly IntPtr hSysTray = WindowsAPI.FindWindowEx(hTray, IntPtr.Zero, "TrayNotifyWnd", null);
        private static readonly IntPtr StartMenu = WindowsAPI.FindWindowEx(hDesktop, IntPtr.Zero, "DV2ControlHost", null);
        private static readonly IntPtr StartMenuBtn = WindowsAPI.FindWindowEx(hTray, IntPtr.Zero, "Button", "start");
        private static readonly IntPtr StartMenuLP = WindowsAPI.FindWindowEx(StartMenu, IntPtr.Zero, "DesktopSFTBarHost", null);
        private static readonly IntPtr StartMenuRP = WindowsAPI.FindWindowEx(StartMenu, IntPtr.Zero, "DesktopSFTBarHost", null);
        private static readonly IntPtr StartMenuUP = WindowsAPI.FindWindowEx(StartMenu, IntPtr.Zero, "Desktop User Pane", null);
        private static readonly IntPtr StartMenuLF = WindowsAPI.FindWindowEx(StartMenu, IntPtr.Zero, "DesktopLogoffPane", null);
        public static IntPtr VistaStart = IntPtr.Zero;
	    public PerPixelAlphaForm ShowDesktop = null;
        private Timer FlushMemmory;
        private Timer timer2;
        private Timer timer3;
        private Timer timer4;
        private Timer ThumbFormClose;
        private Timer UpdateTimer;
	    private List<IntPtr> LL = null;
	    private Form Rsender = null;
        private ToolStripMenuItem miUpdateCheck;
        private ToolStripMenuItem aboutToolStripMenuItem;
        private DS ds1;
        public int TaskbarposX = 0;
        public int TaskbarPosY = 0;
        public ListView LVRecent = new ListView();
	    public bool ForegroundB;
        const string sRecentKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RecentDocs";
        const string sFolderKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RecentDocs\\Folder";
        const char ControlA = (char)1;
        System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
        static string PwString = "123";
        static char[] PasswordChars = PwString.ToCharArray();
        SecureString Password = new SecureString();

                   
        /// <summary>Path to special My Recent Documents directory</summary>
        string MyRecentDocsFolder;

        /// <summary>Top level Registry key</summary>
          RegistryKey CurrentUserKey;

        /// <summary>Registry key for RecentDocs</summary>
          RegistryKey RecentKey;

        /// <summary>Registry key for RecentDocs/Folder</summary>
          RegistryKey FolderKey;

        private Image Fico = null;
        /// <summary>
        /// This contains the keyValues and MRUListEx under the RecentDocs registry key
        /// </summary>
          RecentDocsFormat RecentDocsFormatToplevel;

          Dictionary<String, RecentDocsFormat> RecentDocsSubkeys;   //file ext name as key, to get KeyBinding
          List<string> RecDoc = new List<string>();
        private Thread t1;
        private System.Data.SQLite.SQLiteConnection sqLiteConnection1;
        private System.Data.SQLite.SQLiteDataAdapter sqLiteDataAdapter1;
        private System.Data.SQLite.SQLiteCommand sqliteSelectCommand1;
        private   System.Data.SQLite.SQLiteConnection sqLiteConn;
        private   System.Data.SQLite.SQLiteDataAdapter sqLiteDataA;
        private   System.Data.SQLite.SQLiteCommand sqliteSelectComm;
	    private   GlobalHooksTest.DS SBarDS;
        private BindingSource recentDocBindingSource;
        private BindingSource bindingSource1;
        private GlobalHooksTest.DSTableAdapters.RecentTableAdapter recentTableAdapter;
        private ListView listView1;
        private ColumnHeader columnHeader1;
        private ColumnHeader columnHeader2;
        private RR.Windows.Forms.ListViewExSort listViewExSort1;
        private Timer trPop;
        private BindingSource bsTaskabrPinned;
        private GlobalHooksTest.DSTableAdapters.TaskbarPinnedTableAdapter taskbarPinnedTableAdapter;
        private GlobalHooksTest.DSTableAdapters.PinnedTableAdapter pinnedTableAdapter1;
        private System.Data.OleDb.OleDbConnection csSbar;
        private Timer tmDrop;
        private Timer tmDropP;
          bool AddStarDotFiles = false;
        private Timer tmAutoHide;
        private Timer tmrClean;
        private ToolStripMenuItem toolStripMenuItem2;
        private ContextMenu mnuTaskbarRightClick;
        private MenuItem miTaskmanager;
        private MenuItem menuItem6;
        private MenuItem menuItem2;
        private MenuItem menuItem3;
        private MenuItem miLockTaskbar;
        private MenuItem menuItem5;
        private MenuItem miCascade;
        private MenuItem miStackedWin;
        private MenuItem miSidebySideWin;
        private GlobalHooksTest.DSTableAdapters.ExclusionTableAdapter exclusionTableAdapter1;
        private Timer tmRefreshDropZone;
        private Timer tmFullScreenDetect;
		private int MaxSize;

        public void GetstartbuttonvistaH()
        {
            uint procId;
            WindowsAPI.GetWindowThreadProcessId(hTray, out procId);
            Process p = Process.GetProcessById((int)procId);
            if (p != null)
            {
                // enumerate all threads of that process...
                foreach (ProcessThread t in p.Threads)
                {
                    WindowsAPI.EnumThreadWindows((uint)t.Id, MyEnumThreadWindowsProc, IntPtr.Zero);
                }
            }
        }

        private   bool MyEnumThreadWindowsProc(IntPtr hWnd, IntPtr lParam)
        {
            StringBuilder buffer = new StringBuilder(256);
            if (WindowsAPI.GetWindowText(hWnd, buffer, buffer.Capacity) > 0)
            {
                if (buffer.ToString() == "Start")
                {
                    VistaStart = hWnd;
                    return false;
                }
            }
            return true;
        }

        #region TaskBarItems
        internal class Window
        {
            public string Title;
            public IntPtr Handle;

            public override string ToString()
            {
                return Title;
            }
        }

        static readonly PerPixelAlphaForm Startbar = new PerPixelAlphaForm();
	    private   IntPtr Foreground = IntPtr.Zero;

        private List<Window> windows;
        static public Point P = new Point(0, 20);
        private Button button1;
        public Point Pr = new Point(P.X + 13, P.Y + 2);
        private Button button3;
	    public bool TT;
        public   Form Pop;
	    private   Form Pop2;
        private   Form TempF;
	    public bool ExitProc;
        public Image baseImage;
        private ToolStripMenuItem toolStripMenuItem1;
        private Timer timer1;
//	    private  Timer Tim;
        private ContextMenuStrip contextMenuStrip1;
        private ToolStripMenuItem optionsToolStripMenuItem;
        private ToolStripMenuItem exitToolStripMenuItem;
        private ToolStripMenuItem developperToolStripMenuItem;
        private Button button2;
        private   WaitableTimer Tim22 = null;
        bool IsAltTabWindow(IntPtr hwnd)
        {
            // Start at the root owner
            IntPtr hwndWalk = WindowsAPI.GetAncestor(hwnd, WindowsAPI.GA_ROOTOWNER);

            // See if we are the last active visible popup
            IntPtr hwndTry;
            while ((hwndTry = WindowsAPI.GetLastActivePopup(hwndWalk)) != hwndTry)
            {
                if (WindowsAPI.IsWindowVisible(hwndTry) != 0) break;
                hwndWalk = hwndTry;
            }
            return hwndWalk == hwnd;
        }

        private void GetWindows()
        {
            windows = new List<Window>();
            WindowsAPI.EnumWindows(Callback, 0);

            int i = 0;
            int j = 0;
            int Count;
//            int PX = 0;
            
            foreach (Window w in windows)
            {

                if (IsAltTabWindow(w.Handle))
                {
                    string s = Path.GetFileName(WindowsAPI.GetWindowModuleFileName(w.Handle)).ToLower();
                    bool Isexist = false;
                    ContainerListViewObject cv = treeListView1.GetItemAt(s);
                    if ((Path.GetFileName(WindowsAPI.GetWindowModuleFileName(w.Handle)).ToLower() != "aerosnap.exe") & (ExclusionList.IndexOf(Path.GetFileName(WindowsAPI.GetWindowModuleFileName(w.Handle).ToLowerInvariant()).ToLowerInvariant())) == -1)
                    {

                        try
                        {
                            Count = treeListView1.Nodes[treeListView1.Nodes.IndexOf(cv)].GetNodeCount(false);
                            Isexist = true;

                        }
                        catch
                        {
                            Isexist = false;
                            Count = 0;
                        }

                        if (Count == 0 & !Isexist)
                        {

                            var t = new PerPixelAlphaForm();
                            var tt = new PerPixelAlphaForm();
                            var tc = new PerPixelAlphaForm();
                            var tln = new TreeListNode(s);
                            var sub = new TreeListNode(WindowsAPI.GetWindowName(w.Handle));
                            t.ShowInTaskbar = false;
                            tc.ShowInTaskbar = false;
                            tt.ShowInTaskbar = false;
                            t.Text = w.Handle.ToString();
                            t.TopMost = true;
                            tt.TopMost = true;
                            tc.TopMost = true;
                            t.AllowDrop = true;
                            tt.AllowDrop = true;
                            t.MouseEnter += t_MouseHover;
                            t.MouseHover += t_MouseHover_Real;
                            t.MouseLeave += t_MouseLeave;
                            tt.MouseEnter += tt_MouseHover;
                            tt.MouseHover += tt_MouseHover_Real;
                            tt.MouseLeave += tt_MouseLeave;
                            t.DragEnter += new DragEventHandler(t_DragEnter);
                            t.DragDrop += new DragEventHandler(t_DragDrop);
                            tt.DragDrop += t_DragDrop;
                            tt.DragEnter += t_DragEnter;
                            t.MouseUp += t_new_Click;
                            tt.MouseUp += tt_MouseUp;
                            tt.Text = w.Handle.ToString();
                            Bitmap ButtonBmp = new Bitmap(ButtonBmpS);
                            t.SetBitmap(ButtonBmp);
                            int ButtonWidth = ButtonBmp.Width;
                            int ButtonHeight = ButtonBmp.Height;
                            Icon I = null;
                            if (ButtonHeight > 34)
                            {
                                if (!File.Exists(ExePath + "\\Skins\\" + SkinDir + "\\" + Path.GetFileNameWithoutExtension(s) + ".png"))
                                {
                                    I = lst.Icon(lst.IconIndex(WindowsAPI.GetWindowModuleFileName(w.Handle)));
                                }
                                else
                                {
                                    Bitmap IconBMP = new Bitmap(ExePath + "\\Skins\\" + SkinDir + "\\" + Path.GetFileNameWithoutExtension(s) + ".png");
                                    I = BitmapToIcon(IconBMP);
                                    IconBMP.Dispose();
                                }
                            }
                            else
                            {
                                if (!File.Exists(ExePath + "\\Skins\\" + SkinDir + "\\" + Path.GetFileNameWithoutExtension(s) + ".png"))
                                {
                                    I = lsts.Icon(lsts.IconIndex(WindowsAPI.GetWindowModuleFileName(w.Handle)));
                                }
                                else
                                {
                                    Bitmap IconBMP = new Bitmap(ExePath + "\\Skins\\" + SkinDir + "\\" + Path.GetFileNameWithoutExtension(s) + ".png");
                                    I = BitmapToIcon(IconBMP);
                                    IconBMP.Dispose();
                                }

                            }
                            ButtonBmp.Dispose();
                            ButtonBmp = null;
//                            Bitmap TransBgr = new Bitmap(TransbgrS);
                            var TransBgrSB = new Bitmap(15, 15);
                            TransBgrSB.MakeTransparent();
//                            TransBgr.Dispose();
//                            TransBgr = null;
                            tc.SetBitmap(TransBgrSB);
                            TransBgrSB.Dispose();
                            TransBgrSB = null;
                            GC.Collect();
                            Bitmap Explorer = new Bitmap(ExePath + "\\Skins\\" + SkinDir + "\\explorer32.png");
                            if (s == "explorer.exe")
                            {
                                tt.SetBitmap(Explorer);
                                Explorer.Dispose();
                                Explorer = null;
                            }
                            else
                            {
                                tt.SetBitmap(I.ToBitmap());
                                Explorer.Dispose();
                                Explorer = null;
                            }
                            double E = (ButtonWidth + 2) / 1.5;
                            
                            t.Location = new Point(PX + (ButtonWidth +2) * i++, P.Y);
//                            tt.Location = new Point(Pr.X + (ButtonWidth + 2) * j++, P.Y + (ButtonHeight - I.Height)/2);
                            tt.Location = new Point(t.Location.X + (ButtonWidth - I.Width) / 2, P.Y + (ButtonHeight - I.Height) / 2);
                            tc.Location = new Point(t.Location.X + (int)E, t.Location.Y + (int)E/2);
                            t.Show(Startbar);
                            tt.Show(t);
                            tc.Show(tt); 
//                            if (CountBadgeBool == 1)
//                            {
//                                tc.Show(tt); 
//                            }
                            if (CountBadgeBool == 1)
                            {
                                WindowsAPI.ShowWindow((int)tc.Handle, 0);
                            }
                            else
                            {
                                WindowsAPI.ShowWindow((int)tc.Handle, 1);
                            }
                            tln.SubItems.Add(t.Handle.ToString());
                            tln.SubItems.Add(tt.Handle.ToString());
                            tln.SubItems.Add("");
                            tln.SubItems.Add("");
                            tln.SubItems.Add("");
                            tln.SubItems.Add(tc.Handle.ToString());
                            tln.SubItems.Add(WindowsAPI.GetWindowModuleFileName(w.Handle).ToLower());
                            tln.SubItems.Add("0");
                            sub.SubItems.Add("");
                            sub.SubItems.Add("");
                            sub.SubItems.Add(w.Handle.ToString());
                            sub.SubItems.Add("0");
                            sub.SubItems.Add("0");
                            sub.SubItems.Add("0");
                            sub.SubItems.Add("0");
                            sub.SubItems.Add("0");
                            uint PID = 0;
                            WindowsAPI.GetWindowThreadProcessId(w.Handle, out PID);
                            sub.SubItems.Add(PID.ToString());
//                            Process.EnterDebugMode();
                            Process p = Process.GetProcessById((int)PID);
                            p.EnableRaisingEvents = true;
                            p.Exited += procs_Exited;
//                            Process.LeaveDebugMode();
                            tln.Nodes.Add(sub);
                            treeListView1.Nodes.Add(tln);
                            I.Dispose();
                            I = null;
                            GC.Collect();
                            

                        }
                        else
                        {

                            TreeListNode tln = treeListView1.Nodes[treeListView1.Nodes.IndexOf(cv)];
                            var sub = new TreeListNode(WindowsAPI.GetWindowName(w.Handle));
                            sub.SubItems.Add("");
                            sub.SubItems.Add("");
                            sub.SubItems.Add(w.Handle.ToString());
                            sub.SubItems.Add("0");
                            sub.SubItems.Add("0");
                            sub.SubItems.Add("0");
                            sub.SubItems.Add("0");
                            sub.SubItems.Add("0");
                            uint PID = 0;
                            WindowsAPI.GetWindowThreadProcessId(w.Handle, out PID);
                            sub.SubItems.Add(PID.ToString());
//                            Process.EnterDebugMode();
                            Process p = Process.GetProcessById((int)PID);
                            p.EnableRaisingEvents = true;
                            p.Exited += procs_Exited;
//                            Process.LeaveDebugMode();
                            tln.Nodes.Add(sub);

                        }
                    } 
                }

            }
            windows.Clear();
            windows = null;
            GC.Collect();

        }
        private void GetTaskbarPinned()
        {
            taskbarPinnedTableAdapter.Update(ds1.TaskbarPinned);
            int i = 0;
            int j = 0;
            foreach (DataRow dr in ds1.TaskbarPinned.Rows)
            {


                string s = dr["EXE_NAME"].ToString();
//                ContainerListViewObject cv = treeListView1.GetItemAt(s);

                var t = new PerPixelAlphaForm();
                var tt = new PerPixelAlphaForm();
                var tc = new PerPixelAlphaForm();
                var tln = new TreeListNode(s);
//                            var sub = new TreeListNode(WindowsAPI.GetWindowName(w.Handle));
                t.ShowInTaskbar = false;
                tc.ShowInTaskbar = false;
                tt.ShowInTaskbar = false;
//                t.Text = w.Handle.ToString();
                t.TopMost = true;
                tt.TopMost = true;
                tc.TopMost = true;
                t.AllowDrop = true;
                tt.AllowDrop = true;
                t.MouseEnter += t_MouseHover;
                t.MouseHover += t_MouseHover_Real;
                t.MouseLeave += t_MouseLeave;
                tt.MouseEnter += tt_MouseHover;
                tt.MouseHover += tt_MouseHover_Real;
                tt.MouseLeave += tt_MouseLeave;
                t.DragEnter += new DragEventHandler(t_DragEnter);
                t.DragDrop += new DragEventHandler(t_DragDrop);
                tt.DragDrop += t_DragDrop;
                tt.DragEnter += t_DragEnter;
                t.MouseUp += t_new_Click;
                tt.MouseUp += tt_MouseUp;
//                            tt.Text = w.Handle.ToString();
                Bitmap ButtonBmpT = new Bitmap(ButtonBmpS);
                Bitmap ButtonBmp = new Bitmap(TransS);
                Bitmap QL = new Bitmap(ButtonBmp, ButtonBmpT.Width, ButtonBmpT.Height);
                ButtonBmpT.Dispose();
                t.SetBitmap(QL);
                int ButtonWidth = QL.Width;
                int ButtonHeight = QL.Height;
                Icon I = null;
                if (ButtonWidth > 59)
                {
                    I = lst.Icon(lst.IconIndex(dr["EXE_PATH"].ToString()));
                }
                else
                {
                    I = lsts.Icon(lsts.IconIndex(dr["EXE_PATH"].ToString()));
                }
                ButtonBmp.Dispose();
                ButtonBmp = null;
                QL.Dispose();
                GC.Collect();
                Bitmap Explorer = new Bitmap(ExePath + "\\Skins\\" + SkinDir + "\\explorer32.png");
                if (s.ToLower() == "explorer.exe")
                {
                    tt.SetBitmap(Explorer);
                    Explorer.Dispose();
                    Explorer = null;
                }
                else
                {
                    tt.SetBitmap(I.ToBitmap());
                    Explorer.Dispose();
                    Explorer = null;
                }
                double E = (ButtonWidth + 2) / 1.5;
                t.Location = new Point(P.X + (ButtonWidth + 2) * i++, P.Y);
                //                            tt.Location = new Point(Pr.X + (ButtonWidth + 2) * j++, P.Y + (ButtonHeight - I.Height)/2);
                tt.Location = new Point(t.Location.X + (ButtonWidth - I.Width) / 2, P.Y + (ButtonHeight - I.Height) / 2);
//                tc.Location = new Point(t.Location.X + (int)E, t.Location.Y + (int)E / 2);
                t.Show(Startbar);
                tt.Show(t);
                tln.SubItems.Add(t.Handle.ToString());
                tln.SubItems.Add(tt.Handle.ToString());
                tln.SubItems.Add("");
                tln.SubItems.Add("");
                tln.SubItems.Add("");
                tln.SubItems.Add("");
                tln.SubItems.Add(dr["EXE_PATH"].ToString());
                tln.SubItems.Add("1");
                treeListView1.Nodes.Add(tln);
                I.Dispose();
                I = null;
                GC.Collect();

            }

        }

	    private void t_DragDrop(object sender, DragEventArgs e)
	    {
            tmDrop.Stop();
            TreeListNode TTT = null;
            for (int i = 0; i < treeListView1.Nodes.Count; i++)
            {
                if (treeListView1.Nodes[i].SubItems[0].Text == (sender as Form).Handle.ToString())
                {
                    TTT = treeListView1.Nodes[i];
                }

            }

	        string ProgPath = WindowsAPI.GetWindowModuleFileName((IntPtr)Convert.ToInt32(TTT.Nodes[0].SubItems[2].Text));
            string[] FileList = (string[])e.Data.GetData(DataFormats.FileDrop, false);

            // Do something with the data...

            // For example add all files into a simple label control:
            foreach (string File in FileList)
            {
                if (Path.GetExtension(File).ToLowerInvariant() == ".png")
                {
                    if (sv.OSMajorVersion != 6)
                    {
                        if (MessageBox.Show("Curently you are trying to drop a png image to this button." + "\r\n" + "Do you want to set button icon(Yes) or pin it in JumpList(No)?", "Set Icon", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                        {
                            System.IO.File.Copy(File, ExePath + "\\Skins\\" + SkinDir + "\\" + Path.GetFileNameWithoutExtension(TTT.Text) + ".png");
                        }
                        else
                        {
                            pinnedTableAdapter1.Insert(Path.GetFileNameWithoutExtension(ProgPath).ToLower(), File);
                            pinnedTableAdapter1.Update(ds1.Pinned);
                            ds1.AcceptChanges();
                        } 
                    }
                    else
                    {
                        
                        TaskDialog TD = new TaskDialog("Curently you are trying to drop a png image to this button." + "\r\n" + "Please choise action?", "Setting Button Icon", "", TaskDialogButton.OK | TaskDialogButton.Cancel, TaskDialogIcon.Information);
                        TD.Width = 250;
                        TD.AllowDialogCancellation = false;
                        TD.RadioButtons = new CustomButton[]{
                            new CustomButton(1, "Change Button Icon"),
	                        new CustomButton(2, "Pin to JumpList")                    
                        };
                        TD.RadioButtonClick += new EventHandler<ClickEventArgs>(TD_RadioButtonClick);
                        Results TDResults = TD.Show();

                        if (TDResults.CommonButton == Result.OK)
                        {
                            if (ChangeIcon)
                            {
                                System.IO.File.Copy(File, ExePath + "\\Skins\\" + SkinDir + "\\" + Path.GetFileNameWithoutExtension(TTT.Text) + ".png");
                            }
                            else
                            {
                                pinnedTableAdapter1.Insert(Path.GetFileNameWithoutExtension(ProgPath).ToLower(), File);
                                pinnedTableAdapter1.Update(ds1.Pinned);
                                ds1.AcceptChanges();
                            } 
                        } 
                    }
                }
                else
                {
                    pinnedTableAdapter1.Insert(Path.GetFileNameWithoutExtension(ProgPath).ToLower(), File);
                    pinnedTableAdapter1.Update(ds1.Pinned);
                    ds1.AcceptChanges();
                }

            }
//	        MessageBox.Show(FileList.Length.ToString());

	    }
        bool ChangeIcon = false;
        void TD_RadioButtonClick(object sender, ClickEventArgs e)
        {
            if (e.ButtonID == 1)
            {
                ChangeIcon = true;
            }
            else
            {
                ChangeIcon = false;
            }
        }

        public   string GetAssoc(string doctype)
        {

            uint pcchOut = 0;
            WindowsAPI.AssocQueryString(WindowsAPI.AssocF.Verify, WindowsAPI.AssocStr.Executable, doctype, null, null, ref pcchOut);
            StringBuilder pszOut = new StringBuilder((int)pcchOut);
            WindowsAPI.AssocQueryString(WindowsAPI.AssocF.Verify, WindowsAPI.AssocStr.Executable, doctype, null, pszOut, ref pcchOut);
            return pszOut.ToString();
        }

	    private void t_DragEnter(object sender, DragEventArgs e)
	    {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Copy; // Okay

                //------
                int Handleint = 0;
                TempF = (sender as Form);
                if (TempF.Owner.Handle == Startbar.Handle)
                {
                    Handleint = 0;
                }
                else
                {
                    Handleint = 1;
                }

                TreeListNode TTT = null;
                for (int i = 0; i < treeListView1.Nodes.Count; i++)
                {
                    if (treeListView1.Nodes[i].SubItems[Handleint].Text == TempF.Handle.ToString())
                    {
                        TTT = treeListView1.Nodes[i];
                    }

                }
                tmDrop.Tag = TTT;
                tmDrop.Start();

                
          //-----
            }
            else
                e.Effect = DragDropEffects.None;
	    }

	    private class WindowShow
	    {
	        public IntPtr TheHandle;
	        public int Command;
            public void ShowWindow()
            {
                WindowsAPI.ShowWindow((int)TheHandle, Command);  
            }
	    }
          void LoadLeftClickMenu(PerPixelAlphaForm TempF, int AllighYL)
        {
            if (RightClickForm != null)
            {
                RightClickForm.Dispose();
                RightClickForm = null;
            }
            if (Pop != null)
            {
                Pop.Dispose();
            }
//            TempF = (sender as Form);

            TreeListNode TTT = null;
            for (int i = 0; i < TLV.Nodes.Count; i++)
            {
                if (TLV.Nodes[i].SubItems[0].Text == TempF.Handle.ToString())
                {
                    TTT = treeListView1.Nodes[i];
                }

            }

            if (TTT.GetNodeCount(false) <= 1)
            {
                if (TTT.GetNodeCount(false) == 1)
                {
                    var winh = (IntPtr)Convert.ToInt32(TTT.Nodes[0].SubItems[2].Text);
                    if ((WindowsAPI.GetWindowLong(winh, WindowsAPI.GWL_STYLE) & WindowsAPI.WS_MINIMIZE) != 0)
                    {
                        WindowsAPI.SetForegroundWindow(winh);
                        Foreground = winh;
                        WindowShow RestWin = new WindowShow();
                        RestWin.TheHandle = winh;
                        RestWin.Command = (int) WindowsAPI.SW.RESTORE;
                        Thread SWindow = new Thread(new ThreadStart(RestWin.ShowWindow));
                        SWindow.Start();
//                        WindowsAPI.ShowWindow((int)winh, (int)WindowsAPI.SW.RESTORE);
                    }
                    else
                    {
                        IntPtr y = WindowsAPI.GetForegroundWindow();

                        if (Foreground != winh)
                        {
                            WindowsAPI.SetForegroundWindow(winh);
                            Foreground = winh;
                        }
                        else
                        {
                            WindowShow RestWin = new WindowShow();
                            RestWin.TheHandle = winh;
                            RestWin.Command = (int)WindowsAPI.SW.MINIMIZE;
                            Thread SWindow = new Thread(new ThreadStart(RestWin.ShowWindow));
                            SWindow.Start();
//                            WindowsAPI.ShowWindow((int)winh, (int)WindowsAPI.SW.MINIMIZE);
                            var F = TempF;
                            Bitmap ButtonBmp = new Bitmap(ButtonBmpS);
                            F.SetBitmap(ButtonBmp);
                            ButtonBmp.Dispose();
                            ButtonBmp = null;
                        }
                    }
                }
                else
                {
//                    WindowsFormsApplication1.Form1.IsExecuted = false;
//                    WindowsFormsApplication1.Form1.ExePath = TTT.SubItems[6].Text;
//                    WindowsFormsApplication1.Form1.ProcStart(false);
//                    WindowsAPI.ShellExecute(IntPtr.Zero, "open", TTT.SubItems[6].Text, "", "",
//                                                    WindowsAPI.ShowCommands.SW_SHOW);
                    Process.Start(TTT.SubItems[6].Text);
//                    WindowsFormsApplication1.Form1.ProcStart(TTT.SubItems[6].Text);
                }

            }
            else
            {
                if (Pop != null)
                {
                    //Pop.Dispose();
                    //                        //Pop.Dispose();
                    Pop = null;
                    //                        if (Tim != null)
                    //                        {
                    //                            Tim.Stop();
                    //                        }
                }
                PopUP = new PerPixelAlphaForm();
                PopUP.StartPosition = FormStartPosition.Manual;
                PopUP.Location = new Point(-500, -500);
                PopUP.TopMost = true;
                PopUP.ShowInTaskbar = false;
                PopUP.Tag = TempF.Handle.ToString();
                int m = 0;
                PopUP.Name = "F_POP";
                PopUP.Text = "F_POP";
                PopUP.AllowDrop = true;
                PopUP.Load += PopUP_Load;
                PopUP.Closed += PopUP_Closed;
                PopUP.Disposed += PopUP_Closed;
                WindowsAPI.RECTW TaskPos = new WindowsAPI.RECTW();
                WindowsAPI.GetWindowRect(hReBar, ref TaskPos);
                List<PerPixelAlphaForm> FF = new List<PerPixelAlphaForm>();

                for (int i = 0; i < TTT.Nodes.Count; i++)
                {
                    var PF = new PerPixelAlphaForm();
                    PF.ShowInTaskbar = false;
                    var B = new Bitmap(PopUpButtonBgr);
                    var BA = new Bitmap(PopUpButtonBgrA);
                    var BAF = new Bitmap(PopUpButtonBgrAF);
                    Graphics g = Graphics.FromImage(B);
                    Graphics ga = Graphics.FromImage(BA);
                    Graphics gf = Graphics.FromImage(BAF);
                    Icon I = null;
                    if (Path.GetFileNameWithoutExtension(WindowsAPI.GetWindowModuleFileName(
                                    (IntPtr)Convert.ToInt32(TTT.Nodes[i].SubItems[2].Text))).ToLower() == "explorer")
                    {
                        SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass();

                        string filename;
                        int WinHandle = 0;
                        string WinPath = "";

                        foreach (SHDocVw.InternetExplorer ie in shellWindows)
                        {
                          
                            filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();
                            WinHandle = ie.HWND;
                            if (filename.Equals("explorer"))
                            {

                                if (ie.HWND == Convert.ToInt32(TTT.Nodes[i].SubItems[2].Text))
                                {
                                    Uri ur = null;
                                    string URL = "";
                                    try
                                    {
                                        ur = new Uri(ie.LocationURL);
                                        URL = Uri.UnescapeDataString(ur.LocalPath);
                                        if (ie.LocationURL == "")
                                        {
                                            URL = ie.LocationName;
                                        }
                                    }
                                    catch
                                    {
                                        try
                                        {

                                            URL = ie.LocationName;

                                        }
                                        catch
                                        {
                                            URL = "";
                                        }
                                        //                        URL = "";
                                    }

                                    //                    MessageBox.Show(Uri.UnescapeDataString(ie.LocationURL));

                                    WinPath = URL;
                                }
                            }
                        }
                        if (WinPath != "")
                        {
                            if (WinPath[1] == Char.Parse(":"))
                            {
                                if (WinPath != Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments))
                                {
                                    I = lsts.Icon(lsts.IconIndex(WinPath, true));
                                }
                                else
                                {
                                    if (sv.OSMajorVersion < 6)
                                    {
                                        I = WindowsAPI.GetSmallIcon(Path.Combine(
                                                Environment.GetFolderPath(Environment.SpecialFolder.System),
                                                   "mydocs.dll"), icDocumentsandsettings);
                                    }
                                    else
                                    {
                                        I = lsts.Icon(lsts.IconIndex(WinPath, true));
                                    }
                                }
                            }
                            else if (WinPath[0] == Char.Parse(@"\") & WinPath[0] == Char.Parse(@"\"))
                            {
                                I = WindowsAPI.GetSmallIcon(Path.Combine(
                                                Environment.GetFolderPath(Environment.SpecialFolder.System),
                                                   "shell32.dll"), 85);
                            }
                            else
                            {
                                //                                I =
                                //                                    lsts.Icon(
                                //                                        lsts.IconIndex(
                                //                                            WindowsAPI.GetWindowModuleFileName(
                                //                                                (IntPtr)Convert.ToInt32(TTT.Nodes[i].SubItems[2].Text))));


                                switch (WinPath)
                                {
                                    case "Control Panel":
                                        I = WindowsAPI.GetSmallIcon(
                                                Path.Combine(
                                                    Environment.GetFolderPath(Environment.SpecialFolder.System),
                                                    "shell32.dll"), icControlPanel);
                                        break;
                                    case "Printers and Faxes":
                                        I = WindowsAPI.GetSmallIcon(
                                                Path.Combine(
                                                    Environment.GetFolderPath(Environment.SpecialFolder.System),
                                                    "shell32.dll"), icPrinterandfaxes);
                                        break;
                                    default:
                                        I =
                                            lsts.Icon(
                                                lsts.IconIndex(
                                                    WindowsAPI.GetWindowModuleFileName(
                                                        (IntPtr)Convert.ToInt32(TTT.Nodes[i].SubItems[2].Text))));
                                        break;
                                }
                                //                                I =
                                //                                    WindowsAPI.GetSmallIcon(
                                //                                        Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System),
                                //                                                     "shell32.dll"), 21);
                            } 
                        }
                        else
                        {
                            I =
                             lsts.Icon(
                                 lsts.IconIndex(
                                     WindowsAPI.GetWindowModuleFileName(
                                         (IntPtr)Convert.ToInt32(TTT.Nodes[i].SubItems[2].Text)))); 
                        }
                    }
                    else
                    {
                        I =
                            lsts.Icon(
                                lsts.IconIndex(
                                    WindowsAPI.GetWindowModuleFileName(
                                        (IntPtr)Convert.ToInt32(TTT.Nodes[i].SubItems[2].Text))));
                    }


                    g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
                    ga.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
                    gf.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
                    SolidBrush sb =
                        new SolidBrush(Color.FromArgb(255,
                                                      Convert.ToInt32(Skin.IniReadValue("TASKBARLEFTCLICK",
                                                                                        "TitleFontColorR")),
                                                      Convert.ToInt32(Skin.IniReadValue("TASKBARLEFTCLICK",
                                                                                        "TitleFontColorG")),
                                                      Convert.ToInt32(Skin.IniReadValue("TASKBARLEFTCLICK",
                                                                                        "TitleFontColorB"))));
                    SolidBrush sba =
                        new SolidBrush(Color.FromArgb(255,
                                                      Convert.ToInt32(Skin.IniReadValue("TASKBARLEFTCLICK",
                                                                                        "TitleFontColorAR")),
                                                      Convert.ToInt32(Skin.IniReadValue("TASKBARLEFTCLICK",
                                                                                        "TitleFontColorAG")),
                                                      Convert.ToInt32(Skin.IniReadValue("TASKBARLEFTCLICK",
                                                                                        "TitleFontColorAB"))));
                    g.DrawImage(I.ToBitmap(), 4, 2);
                    ga.DrawImage(I.ToBitmap(), 4, 2);
                    string WinTitle =
                        WindowsAPI.GetWindowName((IntPtr)Convert.ToInt32(TTT.Nodes[i].SubItems[2].Text));
                    if (g.MeasureString(WinTitle, new Font("Calibri", 10)).Width > 160 - 50)
                    {
                        while (g.MeasureString(WinTitle, new Font("Calibri", 10)).Width > 160 - 50)
                        {
                            WinTitle = WinTitle.Substring(0, WinTitle.Length - 1);
                        }
                        WinTitle = WinTitle + "...";
                    }
                    ga.DrawString(WinTitle,
                        new Font("Calibri", 10), sba, new PointF(24, 2));
                    gf.DrawImage(I.ToBitmap(), 4, 2);
                    gf.DrawString(WinTitle,
                        new Font("Calibri", 10), sb, new PointF(24, 2));
                    g.DrawString(WinTitle,
                                 new Font("Calibri", 10), sb, new PointF(24, 2));
                    PF.Tag = TTT.Nodes[i].SubItems[2].Text;
                    if (Foreground == (IntPtr)Convert.ToInt32(TTT.Nodes[i].SubItems[2].Text))
                    {
                        PF.SetBitmap(BA);
                    }
                    else
                    {
                        PF.SetBitmap(B);
                    }
                    if (TTT.Nodes[i].SubItems[7].Text == "Flashing")
                    {
                        PF.SetBitmap(BAF);
                    }
                    B.Dispose();
                    g.Dispose();
                    BA.Dispose();
                    ga.Dispose();
                    gf.Dispose();
                    I.Dispose();
                    PF.TopMost = true;
                    PF.AllowDrop = true;
                    PF.MouseHover += new EventHandler(PF_MouseHover);
                    PF.DragEnter += new DragEventHandler(PF_DragEnter);
                    PF.DragLeave += new EventHandler(PF_DragLeave);
                    
                    PF.StartPosition = FormStartPosition.Manual;

                    //                        PF.Show(PopUP);
                    m = PF.Width;
                    int PosPFX = TempF.
                                     Location.X - PF.Width / 2 + TempF.Width / 2;
                    int PFX = 0;
                    int PFXX = 0;
                    if (PosPFX < 60)
                    {
                        PFX = 2;
                        PFXX = 45;
                    }
                    else
                    {
                        PFX = PosPFX;
                        PFXX = 16;
                    }
                    //                        PF.Location = new Point(
                    //                            PopUP.Location.X + 60, TaskPos.Location.Y - (PF.Height + 2)*i - 30 - 23);
                    PF.MouseHover += B_MouseHover;
                    PF.MouseLeave += B_MouseLeave;
                    PF.Click += B_Click;
                    FF.Add(PF);
                    //                        PerPixelAlphaForm PFF = new PerPixelAlphaForm();
                    //                        PFF.TopMost = true;
                    //                        PFF.ShowInTaskbar = false;
                    //                        PFF.Tag = (sender as PerPixelAlphaForm).Tag;
                    //                        Bitmap PFFB = new Bitmap(FlyoutbgrWindowCloseS);
                    //                        PFF.SetBitmap(PFFB,0);
                    //                        PFF.Location = new Point(PF.Right - PFFB.Width - 2,
                    //                                                 PF.Location.Y +
                    //                                                 (PF.Height - PFFB.Height) / 2);
                    //                        PFF.Click += new EventHandler(PFF_Click);
                    //                        PFF.MouseLeave += new EventHandler(PFF_MouseLeave);
                    //                        PFF.MouseHover += new EventHandler(PFF_MouseHover);
                    //                        PFF.Show((sender as PerPixelAlphaForm));
                    //                        PFFB.Dispose();
                }


                //                    var PB = new Bitmap(PopUpFormBgr, m + 4, TTT.Nodes.Count*(22) + 6);
                Bitmap FB = new Bitmap(Menu1S);
                var PB = FlyBGR(FB, 60, m + 4 + 85, TTT.Nodes.Count * (24) + 6 + 65 + 4);
                FB.Dispose();
                PopUP.SetBitmap(PB);
                PopUP.Show();
                IntPtr hr = IntPtr.Zero;
                if (sv.Version.Major == 6)
                {
                    hr = WindowsAPI.CreateRoundRectRgn(52, 33, PB.Width - 20, PB.Height - 10, 10, 10);

                }
                WindowsAPI.DWM_BLURBEHIND dbb;
                dbb.fEnable = true;
                dbb.dwFlags = 1 | 2;
                dbb.hRgnBlur = hr;
                dbb.fTransitionOnMaximized = false;

                if (sv.Version.Major == 6)
                {
                    WindowsAPI.DwmEnableBlurBehindWindow(PopUP.Handle, ref dbb);
                }
                PB.Dispose();
                int PopUpX = TempF.Location.X - PopUP.Width / 2 +
                             TempF.Width / 2;
                int PX = 0;
                if (PopUpX < 60)
                {
                    PX = -12;
                }
                else
                {
                    PX = PopUpX;
                }

                Pop = PopUP;

                PopUP.Location = new Point(PX, TaskPos.Location.Y - PopUP.Height - 5 + AllighYL);
                int j = 0;
                foreach (PerPixelAlphaForm ff in FF)
                {
                    ff.Location = new Point(
                        PX + 60, TaskPos.Location.Y - (ff.Height + 2) * j - 30 - 23 + AllighYL);
                    ff.Show(PopUP);
                    j++;
                }

            }
        }

        void PF_DragLeave(object sender, EventArgs e)
        {
            tmDropP.Stop();
        }

        void PF_DragEnter(object sender, DragEventArgs e)
        {
            
//            var winh = (IntPtr)Convert.ToInt32((sender as PerPixelAlphaForm).Tag);
            tmDropP.Tag = (sender as PerPixelAlphaForm).Tag;
            tmDropP.Start();
//            if ((WindowsAPI.GetWindowLong(winh, WindowsAPI.GWL_STYLE) & WindowsAPI.WS_MINIMIZE) != 0)
//            {
//                WindowsAPI.SetForegroundWindow(winh);
//                Foreground = winh;
//                WindowsAPI.ShowWindow((int)winh, (int)WindowsAPI.SW.RESTORE);
//            }
//            else
//            {
//                WindowsAPI.SetForegroundWindow(winh);
//                Foreground = winh;
//            }
        }

        private void t_new_Click(object sender, MouseEventArgs e)
	    {
            int AllighYL = 0;
            try
            {
                AllighYL = Convert.ToInt32(Skin.IniReadValue("TASKBARLEFTCLICK", "YAlligment"));
            }
            catch
            {

                MessageBox.Show("Invalid Skin");

            }
            int AllighY = 0;
            try
            {
                AllighY = Convert.ToInt32(Skin.IniReadValue("TASKBARRIGHTCLICK", "YAlligment"));
            }
            catch
            {

                MessageBox.Show("Invalid Skin");

            }
            IsLeaved = true;
            if (Pop2 != null)
            {
                Pop2.Dispose();
                Pop2 = null;
            }
            if (e.Button == MouseButtons.Left)
            {
                LoadLeftClickMenu((sender as PerPixelAlphaForm),AllighYL);
            }
            else if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                if (TNew != null)
                {
                    TNew.Stop();
                }
                if (RightClickForm != null)
                {
                    RightClickForm.Dispose();
                    RightClickForm = null;
                }
                TreeListNode T = null;
                var L = new List<IntPtr>();
                for (int i = 0; i < treeListView1.Nodes.Count; i++)
                {
                    if (treeListView1.Nodes[i].SubItems[0].Text == (sender as Form).Handle.ToString())
                    {
                        T = treeListView1.Nodes[i];
                    }
                }
                if (T != null)
                {
                    for (int j = 0; j < T.Nodes.Count; j++)
                    {
                        L.Add((IntPtr)Convert.ToInt32(T.Nodes[j].SubItems[2].Text));
                    }
                }
                //                Thread t = new Thread(new ThreadStart(StartRightClickForm));
                //                t.Start();
                //                LL = L;
                //                Rsender = sender as Form;
                //                RKF = TempF;
                PopUpForm.Handles = L;
                int PinnedIs = 0;
                try
                {
                    PinnedIs = Convert.ToInt32(T.SubItems[7].Text);
                }
                catch
                {

                    PinnedIs = 0;
                }
                PopUpForm.IsPinned = PinnedIs;
                PopUpForm.TRV = treeListView1;
                PopUpForm.PinExePath = T.SubItems[6].Text;
                PopUpForm.RecDocs = RecDoc;
                PopUpForm.URL = URL;
                PopUpForm.Title = Title;
                PopUpForm.IsLeave = false;
                RightClickForm = new PerPixelAlphaForm();
                RightClickForm.ShowInTaskbar = false;
                RightClickForm.TopMost = true;
                PopUpForm.G = RightClickForm;
                //                RightClickForm.SetBitmap(new Bitmap(ButtonBmpS));
                RightClickForm.Location = new Point(-500, -500);
                WindowsAPI.ShowWindow((int)RightClickForm.Handle, 0);
                RightClickForm.Show();
                WindowsAPI.ShowWindow((int)RightClickForm.Handle, 0);
                var p = new PopUpForm();
                p.StartPosition = FormStartPosition.Manual;
                p.Location = new Point(-500, -500);
                p.Tag = (sender as Form);
                p.Show(RightClickForm);
                PForm = p;
                PForm.Tag = (sender as Form);
                if ((TempF.Location.X + TempF.Width - p.Width / 2) < 0)
                {
                    p.Location = new Point(2, TempF.Location.Y - p.Height - 55 + AllighY);

                }
                else
                {
                    p.Location = new Point(TempF.Location.X + TempF.Width - p.Width / 2, TempF.Location.Y - p.Height - 55 + AllighYL);
                }
                RightClickForm.Location = new Point(p.Location.X - 60, p.Location.Y - 40);
                WindowsAPI.ShowWindow((int)RightClickForm.Handle, 1);  
            }
            else if (e.Button == MouseButtons.Middle)
            {
                TreeListNode TT = null;
                string ExPath = "";
                for (int k = 0; k < treeListView1.Nodes.Count; k++)
                {
                    if (treeListView1.Nodes[k].SubItems[0].Text == (sender as Form).Handle.ToString())
                    {
                        TT = treeListView1.Nodes[k];
                    }
                }
                if (TT != null)
                {
                    try
                    {
                        ExPath = WindowsAPI.GetWindowModuleFileName((IntPtr)Convert.ToInt32(TT.Nodes[0].SubItems[2].Text));
                        StartNewInstance(ExPath);
                    }
                    catch
                    {

                    }
                }

            }
           
	    }


	    void t_Click(object sender, EventArgs e)
	    {
           
//            TempF = (sender as Form);
//	        if (Pop2 != null)
//	        {
//	           Pop2.Dispose(); 
//	        }
//            
//            TreeListNode TTT = null;
//            for (int i = 0; i < treeListView1.Nodes.Count; i++)
//            {
//                if (treeListView1.Nodes[i].SubItems[1].Text == TempF.Handle.ToString())
//                {
//                    TTT = treeListView1.Nodes[i];
//                }
//
//            }
//
//	       if (TTT.GetNodeCount(false) <=1)
//	       {
//	           IntPtr winh = (IntPtr)Convert.ToInt32(TTT.Nodes[0].SubItems[2].Text);
//               if ((WindowsAPI.GetWindowLong(winh, WindowsAPI.GWL_STYLE) & WindowsAPI.WS_MINIMIZE) != 0)
//               {
//                   WindowsAPI.SetForegroundWindow(winh);
//                   Foreground = winh;
//                   WindowsAPI.ShowWindow((int) winh, (int) WindowsAPI.SW.RESTORE);
//               }
//               else
//               {
//
//                   if (Foreground != winh)
//                       {
//                          WindowsAPI.SetForegroundWindow(winh);
//                           Foreground = winh;
//                       }
//                       else
//                       {
//                           WindowsAPI.ShowWindow((int)winh, (int)WindowsAPI.SW.MINIMIZE);
//                           try
//                           {
//                               PerPixelAlphaForm F = (sender as PerPixelAlphaForm).Owner as PerPixelAlphaForm;
//                               F.SetBitmap(ButtonBmp);
//                           }
//                           catch
//                           {
//
//                           }
//                       }
//               }
//
//	       }
//           else
//	       {
//	           if (Pop != null)
//	           {
//	             //Pop.Dispose();
//                 //Pop.Dispose();
//	               if (Tim != null)
//	               {
//	                   Tim.Enabled = false; 
//	               }
//	           }
//               PopUP = new PerPixelAlphaForm();
//	           PopUP.StartPosition = FormStartPosition.Manual;
//               PopUP.Location = new Point(-500,-500);
//	           PopUP.TopMost = true;
//	           PopUP.ShowInTaskbar = false;
//	           PopUP.Tag = (sender as Form).Owner.Handle.ToString();
//	           int m = 0;
//	           PopUP.Name = "F_POP";
//	           PopUP.Text = "F_POP";
//               PopUP.Load += PopUP_Load;
//               PopUP.Closed += PopUP_Closed;
//	           PopUP.Disposed += PopUP_Closed;
//
//                   for (int i = 0; i < TTT.Nodes.Count; i++)
//                   {
//                       PerPixelAlphaForm PF = new PerPixelAlphaForm();
//                       PF.ShowInTaskbar = false;
//                       Bitmap B = new Bitmap(PopUpButtonBgr);
//                       Bitmap BA = new Bitmap(PopUpButtonBgrA);
//                       Graphics g = Graphics.FromImage(B);
//                       Graphics ga = Graphics.FromImage(BA);
//                       Icon I = lsts.Icon(lsts.IconIndex(WindowsAPI.GetWindowModuleFileName((IntPtr)Convert.ToInt32(TTT.Nodes[i].SubItems[2].Text))));
//
//                       
//                       g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
//                       ga.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
//                      
//                       g.DrawImage(I.ToBitmap(),4,2);
//                       ga.DrawImage(I.ToBitmap(),4,2);
//                       ga.DrawString(WindowsAPI.GetWindowName((IntPtr)Convert.ToInt32(TTT.Nodes[i].SubItems[2].Text)), new Font("Calibri", 10), Brushes.WhiteSmoke, new PointF(24, 2));
//                       g.DrawString(WindowsAPI.GetWindowName((IntPtr)Convert.ToInt32(TTT.Nodes[i].SubItems[2].Text)), new Font("Calibri", 10), Brushes.WhiteSmoke, new PointF(24, 2));
//                       PF.Tag = TTT.Nodes[i].SubItems[2].Text;
//                       if (WindowsAPI.GetForegroundWindow() == (IntPtr)Convert.ToInt32(TTT.Nodes[i].SubItems[2].Text))
//                       {
//                          PF.SetBitmap(BA);
//                       }
//                       else
//                       {
//                          PF.SetBitmap(B); 
//                       }
//                       
//                       B.Dispose();
//                       g.Dispose();
//                       BA.Dispose();
//                       ga.Dispose();
//                       I.Dispose();
//                       PF.TopMost = true;
//                       PF.StartPosition = FormStartPosition.Manual;
//                       PF.Show(PopUP);
//                       m = PF.Width;
//                       int PosPFX = (sender as PerPixelAlphaForm).
//                                    Location.X - PF.Width/2 +(sender as PerPixelAlphaForm).Width/2;
//                       int PFX = 0;
//                       if (PosPFX < 2)
//                       {
//                           PFX = 2; 
//                       }
//                       else
//                       {
//                           PFX = PosPFX; 
//                       }
//                       PF.Location = new Point(
//                           PFX, (sender as PerPixelAlphaForm).Location.Y - PF.Height * i - 25);
//                       PF.MouseHover += B_MouseHover;
//                       PF.MouseLeave += B_MouseLeave;
//                       PF.Click += B_Click;
//                       
//                   }
//
//
//               Bitmap PB = new Bitmap(PopUpFormBgr,m + 4,TTT.Nodes.Count * (22) + 6);
//               PopUP.SetBitmap(PB);
//               PopUP.Show();
//               PB.Dispose();
//	           int PopUpX = (sender as PerPixelAlphaForm).Location.X - PopUP.Width/2 +
//	                        (sender as PerPixelAlphaForm).Width/2;
//	           int PX = 0;
//	           if (PopUpX < 0)
//	           {
//	               PX = 1;
//	           }
//	           else
//	           {
//	               PX = PopUpX;
//	           }
//
//	           Pop = PopUP;
//
//               PopUP.Location = new Point( PX, (sender as PerPixelAlphaForm).Location.Y - PopUP.Height - 0);
//
//	       }
	    
	    }

        void B_MouseLeave(object sender, EventArgs e)
        {
//            Tim.Interval = 1000;
//            Tim.Tick += T_Tick;
//            Tim.Stop();
            Rectangle R = new Rectangle((sender as PerPixelAlphaForm).Location.X,
                                        (sender as PerPixelAlphaForm).Location.Y, (sender as PerPixelAlphaForm).Width,
                                        (sender as PerPixelAlphaForm).Height);
            if (!R.Contains(MousePosition))
            {
                try
                {
                    (sender as PerPixelAlphaForm).OwnedForms[0].Dispose();
                }
                catch
                {
                }
            }
            
        }

        void B_MouseHover(object sender, EventArgs e)
        {
//            Tim.Stop();
        }

	    void B_Click(object sender, EventArgs e)
        {
            (sender as Form).Owner.Dispose();
            var Temp = sender as PerPixelAlphaForm;
            var winh = (IntPtr) Convert.ToInt32(Temp.Tag);
            if ((WindowsAPI.GetWindowLong(winh, WindowsAPI.GWL_STYLE) & WindowsAPI.WS_MINIMIZE) != 0)
            {
                WindowsAPI.SetForegroundWindow(winh);
                Foreground = winh;
                WindowsAPI.ShowWindow((int)winh, (int)WindowsAPI.SW.RESTORE);
            }
            else
            {
                if (Foreground != winh)
                {
                    WindowsAPI.SetForegroundWindow(winh);
                    Foreground = winh;
                }
                else
                {
                    WindowsAPI.ShowWindow((int)winh, (int)WindowsAPI.SW.MINIMIZE);
                }
            }
        }
        
          void PopUP_Closed(object sender, EventArgs e)
        {
//            Tim.Stop();
//            Tim.Dispose();
//            (sender as Form).Dispose();
//            PopUP = null;
            Pop = null;
            trPop.Stop();
        }

        void PopUP_Load(object sender, EventArgs e)
        {
//            Tim = new Timer();
//            Tim.Interval = 1000;
//            Tim.Tick += T_Tick;
//            Tim.Stop();
//            IntPtr h = (IntPtr) ((sender as PerPixelAlphaForm).Tag);
            trPop.Tag = (IntPtr)(Convert.ToInt32((sender as PerPixelAlphaForm).Tag));
            trPop.Start();

        }

        void T_Tick(object sender, EventArgs e)
        {
            if (Pop != null)
            {
                //Pop.Dispose(); 
            }
//            if (Tim != null)
//            {
//                Tim.Stop();
//            }
        }

        void tt_MouseLeave(object sender, EventArgs e)
        {
            PerPixelAlphaForm Frm = (sender as PerPixelAlphaForm).Owner as PerPixelAlphaForm;
            Rectangle RForm = new Rectangle(Frm.Location.X, Frm.Location.Y, Frm.Width, Frm.Height);
            if (!RForm.Contains(MousePosition))
            {
                IsLeaved = true;

                var F = (sender as PerPixelAlphaForm).Owner as PerPixelAlphaForm;
                TreeListNode TTT = null;
                for (int i = 0; i < treeListView1.Nodes.Count; i++)
                {
                    if (treeListView1.Nodes[i].SubItems[0].Text == F.Handle.ToString())
                    {
                        TTT = treeListView1.Nodes[i];
                    }

                }
                string S = "";
                if (F.Tag != null)
                {
                    S = F.Tag.ToString();
                }
                //            int Pinned = 0;
                //            try
                //            {
                //                Pinned = Convert.ToInt32(TTTT.SubItems[7].Text);
                //            }
                //            catch
                //            {
                //
                //                Pinned = 0;
                //            }
                if (TTT.Nodes.Count > 0)
                {
                    if (F != T & S != "Flashing")
                    {
                        //                Bitmap ButtonBmp = new Bitmap(ButtonBmpS);
                        Bitmap ButtonBmp = new Bitmap(ButtonBmpS);
                        Bitmap ButtonBmp2 = new Bitmap(ButtonBmpS2);
                        Bitmap ButtonBmp3 = new Bitmap(ButtonBmpS3);
                        if (TTT.Nodes.Count == 2)
                        {
                            F.SetBitmap(ButtonBmp2);
                        }
                        else if (TTT.Nodes.Count > 2)
                        {
                            F.SetBitmap(ButtonBmp3);
                        }
                        else if (TTT.Nodes.Count > 0)
                        {
                            F.SetBitmap(ButtonBmp);
                        }
                        ButtonBmp2.Dispose();
                        ButtonBmp3.Dispose();

                        //                F.SetBitmap(ButtonBmp);
                        ButtonBmp.Dispose();
                        ButtonBmp = null;
                    }
                    //            if (Tim != null)
                    //            {
                    //                Tim.Start();
                    //            }
                    if (FHandle != (sender as Form).Owner.Handle)
                    {

                        if (Tim22 != null)
                        {
                            Tim22.Start();
                        }
                    }
                }
                else
                {
                    Bitmap ButtonBmpT = new Bitmap(ButtonBmpS);
                    Bitmap ButtonBmp = new Bitmap(TransS);
                    Bitmap QL = new Bitmap(ButtonBmp, ButtonBmpT.Width, ButtonBmpT.Height);
                    ButtonBmpT.Dispose();
                    ButtonBmp.Dispose();
                    F.SetBitmap(QL);
                }
            }

        }
        public struct PopUpData
        {
            public TreeListNode TTT;
            public int MaxSize;
            public float ScaleW;
            public PerPixelAlphaForm Sender;
            public int OrigW;
            public int OrigH;
            public List<PerPixelAlphaForm> PAF;
            public List<PerPixelAlphaForm> PAFC;  
        }

	    private IntPtr thumb = IntPtr.Zero;
        private void UpdateThumb(IntPtr thumb, int L, int T, int R, int B, int wi, int he)
        {
            if (thumb != IntPtr.Zero)
            {
                WindowsAPI.PSIZE size;
                WindowsAPI.DwmQueryThumbnailSourceSize(thumb, out size);

                WindowsAPI.DWM_THUMBNAIL_PROPERTIES props = new WindowsAPI.DWM_THUMBNAIL_PROPERTIES();
                props.dwFlags = (int)WindowsAPI.DwmThumbnailFlags.DWM_TNP_SOURCECLIENTAREAONLY | (int)WindowsAPI.DwmThumbnailFlags.DWM_TNP_RECTDESTINATION | (int)WindowsAPI.DwmThumbnailFlags.DWM_TNP_OPACITY | (int)WindowsAPI.DwmThumbnailFlags.DWM_TNP_VISIBLE;

                props.fVisible = true;
                props.opacity = 255;
                props.fSourceClientAreaOnly = true;

                props.rcDestination = new WindowsAPI.Rect(L, T, R, B);
                if (size.x < wi)
                    props.rcDestination.Right = props.rcDestination.Left + size.x;
                if (size.y < he - 30)
                    props.rcDestination.Bottom = props.rcDestination.Top + size.y;

                WindowsAPI.DwmUpdateThumbnailProperties(thumb, ref props);
            }
        }

        private float G = 0;
        private int GT = 0;
        
        public void LoadPop(Form Sender)
        {
            TreeListNode TTT = null;
            for (int i = 0; i < treeListView1.Nodes.Count; i++)
            {
                if (treeListView1.Nodes[i].SubItems[1].Text == Sender.Handle.ToString())
                {
                    TTT = treeListView1.Nodes[i];
                }

            }
            if (TTT != null & TTT.Nodes.Count >= 1)
            {
                int AllighY = 0;
                string FontStyle = "Calibri";
                int FontSize = 10;
                int FontColorR = 0;
                int FontColorG = 0;
                int FontColorB = 0;
                int TitleX = 24;
                int TitleY = 5;
                int IsBorder = 0;
                try
                {
                    AllighY = Convert.ToInt32(Skin.IniReadValue("TASKBARPREVIEW", "YAlligment"));
                }
                catch
                {

                    MessageBox.Show("Invalid Skin");

                }
                try
                {
                    FontSize = Convert.ToInt32(Skin.IniReadValue("TASKBARPREVIEW", "TitleFontSize"));
                }
                catch
                {

                    MessageBox.Show("Invalid Skin");
                }
                try
                {
                    FontColorR = Convert.ToInt32(Skin.IniReadValue("TASKBARPREVIEW", "TitleFontColorR"));
                }
                catch
                {

                    MessageBox.Show("Invalid Skin");
                }
                try
                {
                    FontColorG = Convert.ToInt32(Skin.IniReadValue("TASKBARPREVIEW", "TitleFontColorG"));
                }
                catch
                {

                    MessageBox.Show("Invalid Skin");
                }
                try
                {
                    FontColorB = Convert.ToInt32(Skin.IniReadValue("TASKBARPREVIEW", "TitleFontColorB"));
                }
                catch
                {

                    MessageBox.Show("Invalid Skin");
                }
                try
                {
                    TitleX = Convert.ToInt32(Skin.IniReadValue("TASKBARPREVIEW", "TitleXPos"));
                }
                catch
                {

                    MessageBox.Show("Invalid Skin");
                }
                try
                {
                    TitleY = Convert.ToInt32(Skin.IniReadValue("TASKBARPREVIEW", "TitleYPos"));
                }
                catch
                {

                    MessageBox.Show("Invalid Skin");
                }
                try
                {
                    IsBorder = Convert.ToInt32(Skin.IniReadValue("TASKBARPREVIEW", "IsBorder"));
                }
                catch
                {

                    MessageBox.Show("Invalid Skin");
                }

                int Scale = Desktop.Top / Desktop.Bottom;

                if (RightClickForm != null)
                {
                    RightClickForm.Dispose();
                    RightClickForm = null;
                }
//                TreeListNode TTT = null;
                for (int i = 0; i < treeListView1.Nodes.Count; i++)
                {
                    if (treeListView1.Nodes[i].SubItems[1].Text == Sender.Handle.ToString())
                    {
                        TTT = treeListView1.Nodes[i];
                    }

                }
                int OrigH = 0;
                int OrigW = 0;
                
                int u = 150;
                string ui = SettingsIni.IniReadValue("Main", "TrackBarThu");
                if (SettingsIni.IniReadValue("Main", "TrackBarThu") != null & SettingsIni.IniReadValue("Main", "TrackBarThu") != "")
                {
                    u = Convert.ToInt32(SettingsIni.IniReadValue("Main", "TrackBarThu"));
                }
                int MaxSize = u - 0;


                float DevCount = Screen.PrimaryScreen.WorkingArea.Width / (MaxSize +  12);
                if (TTT.Nodes.Count >= DevCount)
               {
                   MaxSize = (Screen.PrimaryScreen.WorkingArea.Width - TTT.Nodes.Count*35) / TTT.Nodes.Count;
               }
                int Cpopup = (Desktop.Top + 4) / (MaxSize + 4);
                try
                {
                    IntPtr hr = IntPtr.Zero;
//                    if (sv.Version.Major == 6)
//                    {
//                        hr = WindowsAPI.CreateRoundRectRgn(52, 35 + 0, TTT.Nodes.Count * MaxSize + 18 + (TTT.Nodes.Count - 1) * 12 + 50,
//                                              (MaxSize / Scale) + 8 + 40, 10, 10);
//                    }

                    

                    var FO = new PerPixelAlphaForm();
                    FO.ShowInTaskbar = false;
                    FO.StartPosition = FormStartPosition.Manual;
                    FO.TopMost = true;
                    FO.MouseHover += FO_MouseHover;
                    FO.MouseLeave += FO_MouseLeave;
                    FO.Disposed += FO_Closed;
                    Bitmap FBR = new Bitmap(Menu1S);
                    float ThuHeight = 0;
                    
                    for (int j = 0; j < TTT.Nodes.Count; j++)
                    {
                        if (sv.Version.Major == 6)
                        {
                            
                            var FE = new PerPixelAlphaForm();
                            FE.Size = new Size((int)MaxSize, (int)MaxSize);
                            IntPtr thumbI = IntPtr.Zero;
                            //                        if (thumb != IntPtr.Zero)
                            WindowsAPI.DwmUnregisterThumbnail(thumbI);
                            int iy = WindowsAPI.DwmRegisterThumbnail(FE.Handle,
                                                                     (IntPtr)
                                                                     Convert.ToInt32(TTT.Nodes[j].SubItems[2].Text),
                                                                     out thumbI);

                            WindowsAPI.PSIZE sizeI;
                            WindowsAPI.DwmQueryThumbnailSourceSize(thumbI, out sizeI);
                            float X = sizeI.x;
                            float Y = sizeI.y;
                            float YUYI = (MaxSize / X) * Y;
//                            MessageBox.Show(YUYI.ToString());
                            if (X > Y)
                            {

                                if (YUYI > ThuHeight)
                                {
                                    ThuHeight = YUYI;
                                }
                            }
                            else
                            {

                                if (ThuHeight < MaxSize)
                                {
                                    ThuHeight = MaxSize;
                                }
                            }
//                            Wind/pose();
                        }
                    }
                    if (sv.Version.Major == 6)
                    {
                        hr = WindowsAPI.CreateRoundRectRgn(52, 35 + 0, TTT.Nodes.Count * MaxSize + 18 + (TTT.Nodes.Count - 1) * 12 + 50,
                                              ((int)ThuHeight / Scale) + 8 + 40 + 20, 10, 10);
                        
                    }
                    WindowsAPI.DWM_BLURBEHIND dbb;
                    dbb.fEnable = true;
                    dbb.dwFlags = 1 | 2;
                    dbb.hRgnBlur = hr;
                    dbb.fTransitionOnMaximized = false;
                    Bitmap FOB = null;
                    if (sv.OSMajorVersion != 6)
                    {
                        FOB = FlyBGR(FBR, 60, TTT.Nodes.Count*MaxSize + 18 + (TTT.Nodes.Count - 1)*12 + 70,
                                         (MaxSize) + 8 + 50);
                    }
                    else
                    {
//                        MessageBox.Show(ThuHeight.ToString());
                        FOB = FlyBGR(FBR, 60, TTT.Nodes.Count * MaxSize + 18 + (TTT.Nodes.Count - 1) * 12 + 70,
                                          ((int)ThuHeight) + 8 + 50 + 20); 
                    }
                    //                FBR.Dispose();
                        //                FBR = null;
                        
                    FO.SetBitmap(FOB);

                        //                FO.Show();
                        //                FOB.Dispose();
                    FOB = null; 
                    
                    FO.Load += FO_Load;
                    FO.Tag = Sender.Owner.Handle;
                    FO.Activated += new EventHandler(FO_Activated);
                    FO.Location = new Point(-1000, -1000);
                    if (sv.Version.Major == 6)
                    {
                        WindowsAPI.DwmEnableBlurBehindWindow(FO.Handle, ref dbb);
                    }
                    //                WindowsAPI.SetWindowPos(FO.Handle, WindowsAPI.HWND_TOPMOST, FO.Location.X, FO.Location.Y, FO.Width,
                    //                                        FO.Height, WindowsAPI.SWP_SHOWWINDOW | WindowsAPI.SWP_NOACTIVATE);


                    if (Pop2 != null)
                    {
                        Pop2.Dispose();
                        Pop2 = null;
                    }
                    FO.Show(TempF);
                    WindowsAPI.ShowWindow((int)FO.Handle, 0);
                    Pop2 = FO;
                    Pop2.Tag = Sender.Owner.Handle;
                    WindowsAPI.RECTW TaskPos = new WindowsAPI.RECTW();
                    WindowsAPI.GetWindowRect(hReBar, ref TaskPos);
                    
                    if (TempF.Location.X + TempF.Width / 2 - FO.Width / 2 < 0)
                    {
                        FO.Location = new Point(2, TaskPos.Location.Y - FO.Height - 5 + AllighY);

                    }
                    else
                    {
                        FO.Location = new Point(TempF.Location.X + TempF.Width / 2 - FO.Width / 2,
                                                TaskPos.Location.Y - FO.Height - 5 + AllighY);
                    }
                    WindowsAPI.ShowWindow((int)FO.Handle, 1);
                    PAFC.Clear();
                    PAF.Clear();
                    PAFB.Clear();
                    PAFBB.Clear();
                    for (int i = 0; i < TTT.Nodes.Count; i++)
                    {

                        var REC = new WindowsAPI.RECTW();
                        WindowsAPI.GetWindowRect((IntPtr)Convert.ToInt32(TTT.Nodes[i].SubItems[2].Text), ref REC);
                        float XLenB = 0;
                        float YLenB = 0;
                        OrigW = REC.Width;
                        OrigH = REC.Height;
                        float OrigWF = OrigW;
                        float OrigHF = OrigH;
                        try
                        {
                            float Ratio = OrigWF / OrigHF;
                            if (REC.Width > REC.Height)
                            {
                                XLenB = MaxSize;
                                YLenB = XLenB / Ratio;
                            }
                            else if (REC.Width < REC.Height)
                            {
                                YLenB = MaxSize;
                                XLenB = MaxSize * Ratio;
                            }
                            else
                            {
                                YLenB = MaxSize;
                                XLenB = MaxSize;
                            }
                        }
                        catch
                        {

                        }

                        var PopUpWindow = new PerPixelAlphaForm();
//                        var PopUpWindowH = new PerPixelAlphaForm();
                        var PopUpWindowB = new PerPixelAlphaForm();
                        var PopUpWindowClose = new PerPixelAlphaForm();
                        PopUpWindow.ShowInTaskbar = false;
                        PopUpWindow.StartPosition = FormStartPosition.Manual;
                        PopUpWindow.TopMost = true;
//                        PopUpWindowH.ShowInTaskbar = false;
//                        PopUpWindowH.StartPosition = FormStartPosition.Manual;
//                        PopUpWindowH.TopMost = true;
                        PopUpWindowB.ShowInTaskbar = false;
                        PopUpWindowB.StartPosition = FormStartPosition.Manual;
                        PopUpWindowB.TopMost = true;
                        PopUpWindowClose.ShowInTaskbar = false;
                        PopUpWindowClose.StartPosition = FormStartPosition.Manual;
                        PopUpWindowClose.TopMost = true;
                        Bitmap TransSB = new Bitmap(TransS);

                        Bitmap WindowB = null;
                        if (sv.OSMajorVersion != 6)
                        {
                            WindowB = new Bitmap(TransSB, MaxSize, (MaxSize/Scale)); 
                        }
                        else
                        {
                            WindowB = new Bitmap(TransSB, MaxSize, ((int) (ThuHeight)/Scale) + 20 );  
                        }
                        //                    var WindowB = new Bitmap(MaxSize, (MaxSize / Scale));
                        //                    WindowB.MakeTransparent();
                        TransSB.Dispose();
                        TransSB = null;
                        Bitmap Temp;
                        int YLenBDivider = 0;
                        if (YLenB >= MaxSize - 27)
                        {
                            YLenBDivider = 22;
                        }
                        else
                        {
                            YLenBDivider = 0;
                        }
                        var F3 = new PerPixelAlphaForm();
                        float gf = 0;
                        float XX = 0;
                        float YY = 0;
                        float UU = 0;
//                        float ThuHeight = 0;
                        if (sv.Version.Major == 6)
                        {
                            IntPtr thumb = IntPtr.Zero;
                            //                        if (thumb != IntPtr.Zero)
                            WindowsAPI.DwmUnregisterThumbnail(thumb);
//                            var F3 = new PerPixelAlphaForm();

                            F3.StartPosition = FormStartPosition.Manual;
                            //                        F3.Location = new Point(FO.Location.X + 8 + (i) * (6 + MaxSize) + (MaxSize - (int)XLenB)/2, FO.Location.Y + 4);
                            F3.ShowInTaskbar = false;
                            F3.Size = new Size((int)MaxSize, (int)MaxSize);
                            F3.TopMost = true;
                            //                        F3.Show(PopUpWindow);
                            PAFBB.Add(F3);
                            System.Drawing.Size S = new Size(MaxSize, MaxSize - 30);
                            int iy = WindowsAPI.DwmRegisterThumbnail(F3.Handle, (IntPtr)Convert.ToInt32(TTT.Nodes[i].SubItems[2].Text), out thumb);

                            WindowsAPI.PSIZE size;
                            WindowsAPI.DwmQueryThumbnailSourceSize(thumb, out size);
                            XX = size.x;
                            YY = size.y;
                            gf = XX / YY;
                            float YUY = (MaxSize/XX)*YY;
                            
                            if (XX > YY)
                            {
                                XLenB = MaxSize;
                                YLenB = XLenB / gf;
                                UU = XLenB - YLenB;

                            }
                            else if (XX < YY)
                            {
                                YLenB = MaxSize;
                                XLenB = MaxSize * gf;
                                UU = YLenB - XLenB;

                            }
                            else
                            {
                                YLenB = MaxSize;
                                XLenB = MaxSize;
                                UU = 0;

                            }
                            int O = 0;
                            if (XX / YY < 1.1)
                            {
                                G = 50;
                                GT = 50;
                                O = 0;
                            }
                            else
                            {
                                G = 40;
                                GT = 26;
                                O = 0;
                            }
                            if (XX / YY < 1)
                            {
                                G = 80;
                                GT = 60;
                                O = 0;
                            }
                            if (XX / YY < 0.9)
                            {
                                G = 80;
                                GT = 80;
                                O = 0;
                            }
                            if (XX / YY < 0.8)
                            {
                                G = 80;
                                GT = 60;
                                O = 7;
                            }
                            if (XX / YY < 0.7)
                            {
                                G = 80;
                                GT = 80;
                                O = 0;
                            }
                            if (XX / YY < 0.6)
                            {
                                G = 80;
                                GT = 60;
                                O = 0;
                            }
                            if (XX / YY < 0.5)
                            {
                                G = 80;
                                GT = 60;
                                O = 10;
                            }
                            if (XX / YY < 0.4)
                            {
                                G = 80;
                                GT = 60;
                                O = 12;
                            }
                            if (XX / YY < 0.3)
                            {
                                G = 80;
                                GT = 60;
                                O = 25;
                            }
                            if (XX / YY < 0.2)
                            {
                                G = 80;
                                GT = 60;
                                O = 25;
                            }
                            if (XX / YY < 0.1)
                            {
                                G = 80;
                                GT = 60;
                                O = 25;
                            }
                            float YXDivider = 0;
                            if (YLenB >= MaxSize - 22)
                            {
                                YLenBDivider = 30;
                                YXDivider = ((MaxSize - ((XLenB / YLenB) * (YLenB - YLenBDivider))) / 2) - ((MaxSize - XLenB) / 2);


                            }
                            else
                            {
                                YLenBDivider = 0;
                                YXDivider = 0;
                            }
                            int DevThu = 0;
                            int DevBorder = 0;

                            if (XX > YY)
                            {
                                DevThu = (int)(((ThuHeight) + 10 - YUY) / 2);
                            }
                            else
                            {
                                DevThu = 0;
                                DevBorder = 10;
                            }
                            F3.Location = new Point(FO.Location.X + 8 + (i) * (12 + MaxSize) + (MaxSize - (int)XLenB) / 2 + (int)G / 4 - O + 50, FO.Location.Y + 4 + DevBorder + DevThu + 55 + 8);//new Point(FO.Location.X + 8 + (i) * (6 + MaxSize) + (MaxSize - (int)XLenB) / 2 + (int)YXDivider, FO.Location.Y + 4 + (MaxSize - (int)YLenB) / 2 + YLenBDivider);
                            //                        F3.Location = new Point(PopUpWindow.Location.X,PopUpWindow.Location.Y);//new Point(FO.Location.X + 8 + (i) * (6 + MaxSize) + (MaxSize - (int)XLenB) / 2 + (int)YXDivider, FO.Location.Y + 4 + (MaxSize - (int)YLenB) / 2 + YLenBDivider);
                            //                        F3.Location = new Point(FO.Location.X + (i) * (6 + MaxSize) + 60, FO.Location.Y + 30 + 50);
                            F3.Size = new Size((int)XLenB, (int)YLenB - 30);
                            if (iy == 0)
                                UpdateThumb(thumb, 0, 0, MaxSize - (int)G / 2, MaxSize - (int)G / 2, MaxSize, MaxSize - (int)G / 2);
                            //                        Bitmap TransBgr = new Bitmap(TransbgrS);
                            //                            UpdateThumb(thumb, 0, 0, F3.Width, F3.Height, (int)XLenB, (int)YLenB); 
                            //                        Bitmap TransBgr = new Bitmap(TransbgrS);
                            Temp = new Bitmap(MaxSize, MaxSize);
                            Temp.MakeTransparent();
                            //                        TransBgr.Dispose();
                            //                        TransBgr = null;
                            GC.Collect();
                        }
                        else
                        {
                            if (!WindowsAPI.IsIconic((IntPtr)Convert.ToInt32(TTT.Nodes[i].SubItems[2].Text)) & ((IntPtr)Convert.ToInt32(TTT.Nodes[i].SubItems[2].Text) == Foreground))
                            {
                                IntPtr SDC = Win32API.GetDC((IntPtr)Convert.ToInt32(TTT.Nodes[i].SubItems[2].Text));
                                IntPtr bmDC = Win32API.CreateCompatibleDC(SDC);
                                IntPtr TempHP = WindowsAPI.CreateCompatibleBitmap(SDC, OrigW, OrigH);
                                WindowsAPI.SelectObject(bmDC, TempHP);
                                WindowsAPI.PrintWindow((IntPtr)Convert.ToInt32(TTT.Nodes[i].SubItems[2].Text), bmDC, 0);
                                Win32API.ReleaseDC((IntPtr)Convert.ToInt32(TTT.Nodes[i].SubItems[2].Text), SDC);
                                Win32API.DeleteDC(bmDC);
                                Temp = Image.FromHbitmap(TempHP);
                            }
                            else
                            {

                                //                            Bitmap TransBgr = new Bitmap(TransbgrS);
                                Temp = new Bitmap(MaxSize, MaxSize);
                                Temp.MakeTransparent();
                                //                            TransBgr.Dispose();
                                //                            TransBgr = null;
                                GC.Collect();
                                GC.WaitForPendingFinalizers();
                                GC.Collect();
                            }

                        }
                        if (sv.OSMajorVersion == 6)
                        {
//                            FO.Location = new Point(FO.Location.X, FO.Location.Y - 15 + ((MaxSize) - ((int)WinHeight)));
//
//                            var FOBI = FlyBGR(FBR, 60, TTT.Nodes.Count * MaxSize + 18 + (TTT.Nodes.Count - 1) * 12 + 70,
//                                               ((int)WinHeight) + 8 + 50);
//                            //                FBR.Dispose();
//                            //                FBR = null;
//                            FO.SetBitmap(FOBI);
//                            //                FO.Show();
//                            //                FOB.Dispose();
//                            FOBI = null;
//                            hr = WindowsAPI.CreateRoundRectRgn(52, 35 + 0, TTT.Nodes.Count * MaxSize + 18 + (TTT.Nodes.Count - 1) * 12 + 50,
//                                              ((int)ThuHeight / Scale) + 8 + 40, 10, 10);
                        }
                        Graphics g = Graphics.FromImage(WindowB);
                        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

                        if (sv.OSMajorVersion != 6)
                        {

                            g.DrawImage(Temp, (MaxSize - XLenB) / 2, (MaxSize - YLenB) / 2 + YLenBDivider, XLenB,
                                        YLenB - YLenBDivider);
                        }

                        Icon I =
                            lsts.Icon(
                                lsts.IconIndex(
                                    WindowsAPI.GetWindowModuleFileName(
                                        (IntPtr)Convert.ToInt32(TTT.Nodes[i].SubItems[2].Text))));
                        if (!File.Exists(ExePath + "\\Thumb\\" + TTT.Nodes[i].SubItems[2].Text + ".png") | (IntPtr)Convert.ToInt32(TTT.Nodes[i].SubItems[2].Text) == Foreground)
                        {
                            string WinTitle =
                                WindowsAPI.GetWindowName((IntPtr)Convert.ToInt32(TTT.Nodes[i].SubItems[2].Text));
                            g.DrawImage(I.ToBitmap(), 5, 5);
                            if (g.MeasureString(WinTitle, new Font(FontStyle, FontSize, System.Drawing.FontStyle.Bold)).Width > MaxSize - 32)
                            {
                                while (g.MeasureString(WinTitle, new Font("Calibri", FontSize, System.Drawing.FontStyle.Bold)).Width > MaxSize - 32)
                                {
                                    WinTitle = WinTitle.Substring(0, WinTitle.Length - 1);
                                }
                                WinTitle = WinTitle + "...";
                            }
                            //                        g.DrawString(
                            //                            WindowsAPI.GetWindowName((IntPtr)Convert.ToInt32(TTT.Nodes[i].SubItems[2].Text)),
                            //                            new Font("Calibri", 10), Brushes.Black, new PointF(24, 5));
                            SolidBrush FontColorBrush = new SolidBrush(Color.FromArgb(255, FontColorR, FontColorB, FontColorG));
                            g.DrawString(
                                WinTitle,
                                new Font(FontStyle, FontSize, System.Drawing.FontStyle.Bold), FontColorBrush, new PointF(TitleX, TitleY));
                        }
                        I.Dispose();
                        I = null;
                        float A = 0;
                        float B = 0;
                        float D = 0;
                        float U = MaxSize - 30;
                        float F = 0;
                        D = MaxSize / U;


                        PopUpWindow.Location = new Point(FO.Location.X + 8 + (i) * (12 + MaxSize) + 51, FO.Location.Y + 4 + 35);
//                        PopUpWindowH.Location = new Point(FO.Location.X + 8 + (i) * (12 + MaxSize) + 53, FO.Location.Y + 4 + 55);

                        PopUpWindowB.Location = new Point(FO.Location.X + 8 + (i) * (12 + MaxSize) + 51, FO.Location.Y + 5 + 35);
                        //                    F3.Location = new Point(PopUpWindow.Location.X, PopUpWindow.Location.Y + 30);
                        if (sv.OSMajorVersion != 6)
                        {
                            if (WindowsAPI.IsIconic((IntPtr)Convert.ToInt32(TTT.Nodes[i].SubItems[2].Text)) | ((IntPtr)Convert.ToInt32(TTT.Nodes[i].SubItems[2].Text) != Foreground))
                            {
                                if (!File.Exists(ExePath + "\\Thumb\\" + TTT.Nodes[i].SubItems[2].Text + ".png"))
                                {
                                    Icon IL =
                                            lst.Icon(
                                                lst.IconIndex(
                                                    WindowsAPI.GetWindowModuleFileName(
                                                        (IntPtr)Convert.ToInt32(TTT.Nodes[i].SubItems[2].Text))));
                                    g.DrawImage(IL.ToBitmap(), MaxSize / 2 - 48, MaxSize / 2 - 48 + YLenBDivider, 96, 96);
                                    IL.Dispose();
                                    IL = null;
                                }
                                else
                                {
                                    Bitmap TempB = new Bitmap(ExePath + "\\Thumb\\" + TTT.Nodes[i].SubItems[2].Text + ".png");
                                    Bitmap TempT = null;
                                    if (TempB.Width != MaxSize | TempB.Height != MaxSize)
                                    {
                                        TempT = new Bitmap(TempB,MaxSize,MaxSize);
                                        WindowB = TempT; 
//                                        TempT.Dispose();
                                    }
                                    else
                                    {
                                        WindowB = TempB;
                                    }
//                                    TempB.Dispose();
//                                    TempT.Dispose();
                                }

                            }
                            else
                            {
                                WindowB.Save(ExePath + "\\Thumb\\" + TTT.Nodes[i].SubItems[2].Text + ".png");
                            }
                            WindowsAPI.ShowWindow((int)FO.Handle, 1);

                        }
                        Temp.Dispose();
                        Temp = null;
                        PopUpWindow.SetBitmap(WindowB);
                        FormSizeButtonClose = WindowB.Width;
                        if (sv.Version.Major == 6)
                        {
                            Bitmap n = new Bitmap(MaxSize, MaxSize);
                            n.MakeTransparent();
                            //                        F3.SetBitmap(CreateTiledImage((int)MaxSize - (int)G / 2, (int)MaxSize - (int)G, n, false)); 
                            F3.SetBitmap(n);
                        }
                        Bitmap FlashBgr = null;
                        if (TTT.Nodes[i].SubItems[7].Text == "Flashing")
                        {
                            FlashBgr = new Bitmap(FlashbgrS);
                        }
                        else
                        {
                            FlashBgr = new Bitmap(MaxSize, MaxSize);
                            FlashBgr.MakeTransparent();
                        }
                        Bitmap FlashBgrR = null;//CreateTiledImage(MaxSize + 2, MaxSize + 2, FlashBgr, false);
                        if (sv.OSMajorVersion == 6)
                        {
                            FlashBgrR  = CreateTiledImage(MaxSize + 2, (int)ThuHeight + 2 + 20, FlashBgr, false);
                        }
                        else
                        {
                            FlashBgrR = CreateTiledImage(MaxSize + 2, MaxSize + 2, FlashBgr, false); 
                        }
                        Graphics fg = Graphics.FromImage(FlashBgrR);
                        if (TTT.Nodes[i].SubItems[7].Text == "Flashing")
                        {
                            DrawRoundedRectangle(fg, new Rectangle(0, 0, FlashBgrR.Width - 1, FlashBgrR.Height - 1), 10,
                                                 Pens.DarkOrange);
                        }
                        else
                        {
                            if (IsBorder != 0)
                            {
                                DrawRoundedRectangle(fg, new Rectangle(0, 0, FlashBgrR.Width - 1, FlashBgrR.Height - 1), 10,
                                                                       Pens.DarkGray);
                            }
                        }
//                        Bitmap HoverBMP = new Bitmap(HoverBgrThu);
//                        Bitmap HoverRealBMP = CreateTiledImage(MaxSize, MaxSize,HoverBMP,false);
//                        PopUpWindowH.SetBitmap(HoverRealBMP,0);
                        PopUpWindowB.SetBitmap(FlashBgrR);
                        FlashBgr.Dispose();
                        FlashBgr = null;
                        fg.Dispose();
//                        HoverBMP.Dispose();
//                        HoverRealBMP.Dispose();
                        FlashBgrR.Dispose();
                        FlashBgrR = null;
                        GC.Collect();
                        g.Dispose();
                        g = null;
                        Bitmap FlyouClBGR = new Bitmap(FlyoutbgrWindowCloseS);
                        PopUpWindowClose.SetBitmap(FlyouClBGR, 0);
                        FlyouClBGR.Dispose();
                        FlyouClBGR = null;
                        PopUpWindow.MouseHover += FO_MouseHoverW;
                        PopUpWindow.MouseLeave += FO_MouseLeaveW;
                        PopUpWindow.Tag = TTT.Nodes[i].SubItems[2].Text;
                        PopUpWindow.Click += PopUpWindow_Click;
                        PopUpWindow.Load += PopUpWindow_Load;
                        PopUpWindowClose.Click += PopUpWindowClose_Click;
                        PopUpWindowClose.MouseHover += FO_MouseHoverC;
                        PopUpWindowClose.Tag = TTT.Nodes[i].SubItems[2].Text;
                        PAF.Add(PopUpWindow);
                        PAFC.Add(PopUpWindowClose);
                        PAFB.Add(PopUpWindowB);
//                        PAFH.Add(PopUpWindowH);
                        WindowB.Dispose();
                        WindowB = null;
                        GC.Collect();
                        GC.WaitForPendingFinalizers();
                        GC.Collect();



                    }
//                    LoadPOP NewLoadFormPOP = new LoadPOP();
//                    NewLoadFormPOP.FO = FO;
//                    Thread TRT = new Thread(new ThreadStart(NewLoadFormPOP.LoadPopForms));
//                    TRT.Start();
                    foreach (PerPixelAlphaForm form in PAF)
                    {
//                        PAFH[PAF.IndexOf(form)].Show(FO);
                        PAFB[PAF.IndexOf(form)].Show(FO);
                        form.Show(PAFB[PAF.IndexOf(form)]);
                        PAFC[PAF.IndexOf(form)].Location = new Point(form.Location.X + form.Width - 20,
                                                              form.Location.Y + 4);
                        //                    PAFB[PAF.IndexOf(form)].Location = new Point(form.Location.X - 1, form.Location.Y - 1);
                        PAFB[PAF.IndexOf(form)].Show();
                        PAFC[PAF.IndexOf(form)].Show(form);
                        
                        if (sv.Version.Major == 6)
                        {
                            PAFBB[PAF.IndexOf(form)].Show(form);
                        }

                        WindowsAPI.ShowWindow((int)PAFC[PAF.IndexOf(form)].Handle, 0);
                        Bitmap FlyouWClBGR = new Bitmap(FlyoutbgrWindowCloseS);
                        PAFC[PAF.IndexOf(form)].SetBitmap(FlyouWClBGR);
                        FlyouWClBGR.Dispose();
                        FlyouWClBGR = null;
                        GC.Collect();
                        GC.WaitForPendingFinalizers();
                        GC.Collect();

                    }
                }
                catch
                {

                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect(); 
            }
        }
        static List<PerPixelAlphaForm> PAF = new List<PerPixelAlphaForm>();
        static List<PerPixelAlphaForm> PAFB = new List<PerPixelAlphaForm>();
        static List<PerPixelAlphaForm> PAFC = new List<PerPixelAlphaForm>();
        static List<PerPixelAlphaForm> PAFBB = new List<PerPixelAlphaForm>();
        static List<PerPixelAlphaForm> PAFH = new List<PerPixelAlphaForm>();
	    private static int FormSizeButtonClose;
	    private class LoadPOP
	    {
	        public PerPixelAlphaForm FO;
            public void LoadPopForms()
            {
                foreach (PerPixelAlphaForm form in PAF)
                {
                    //                        PAFH[PAF.IndexOf(form)].Show(FO);
                    try
                    {
                        PAFB[PAF.IndexOf(form)].Show(FO);
                        //                    Thread TT = new Thread(new ParameterizedThreadStart(StartF1));
                        //                    TT.Start(form);
                        form.Show(PAFB[PAF.IndexOf(form)]);

                        //                    PAFB[PAF.IndexOf(form)].Location = new Point(form.Location.X - 1, form.Location.Y - 1);
                        //                    Thread TT3 = new Thread(new ThreadStart(PAFB[PAF.IndexOf(form)].Show));
                        //                    TT3.Start();
                        //PAFB[PAF.IndexOf(form)].Show();
                        //                    Thread TT2 = new Thread(new ParameterizedThreadStart(StartF2));
                        //                    TT2.Start(form);
                        PAFC[PAF.IndexOf(form)].Show(form);

                        if (sv.Version.Major == 6)
                        {
                            PAFBB[PAF.IndexOf(form)].Show(form);
                        }
                        PAFC[PAF.IndexOf(form)].Location = new Point(form.Location.X + FormSizeButtonClose - 20,
                                                              form.Location.Y + 4);
                        WindowsAPI.ShowWindow((int)PAFC[PAF.IndexOf(form)].Handle, 0);
                        Bitmap FlyouWClBGR = new Bitmap(FlyoutbgrWindowCloseS);
                        PAFC[PAF.IndexOf(form)].SetBitmap(FlyouWClBGR);
                        FlyouWClBGR.Dispose();
                        FlyouWClBGR = null;
                    }
                    catch
                    {

                    }
                    
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();

                }
            }
	    }

        public static void StartF1 (object FFO)
        {
            PerPixelAlphaForm form = FFO as PerPixelAlphaForm;
            form.Show(PAFB[PAF.IndexOf(form)]);  
        }
        public static void StartF2(object FFO)
        {
            PerPixelAlphaForm form = FFO as PerPixelAlphaForm;
            PAFC[PAF.IndexOf(form)].Show(form);
        }
        void FO_Activated(object sender, EventArgs e)
        {
//            WindowsAPI.SetActiveWindow(IntPtr.Zero);
        }
          List<string> URL = new List<string>();
          List<string> Title = new List<string>();
        public void GetFirefoxHistory()
        {
            URL.Clear();
            Title.Clear();
            if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
                            @"\Mozilla\Firefox\profiles.ini"))
            {
                sqLiteConnection1.Close();
                IniFile i =
                    new IniFile(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
                                @"\Mozilla\Firefox\profiles.ini");
                string ProfilePath = i.IniReadValue("Profile0", "Path");
                System.IO.File.Copy(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Mozilla\Firefox\" + ProfilePath + @"\places.sqlite", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Mozilla\Firefox\Profiles\places.sqlite", true);
                sqLiteConnection1.ConnectionString =
                    "data source=" + Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Mozilla\Firefox\Profiles\places.sqlite";
                sqLiteConnection1.Open();
                ds1.AcceptChanges();
                sqLiteDataAdapter1.Fill(ds1.moz_historyvisits);
                ////            SQLiteBase db = new SQLiteBase(@"C:\Documents and Settings\dimitar.cenev\Application Data\Mozilla\Firefox\Profiles\0duqfu8a.default\places.sqlite");
                //            DataTable table = db.ExecuteQuery("SELECT Unique ID,URL,Title,Last visit date FROM moz_history LIMIT 20 ORDER BY Last visit date desc;");
                //            DataTable db;

                // closes the database
                //            DataTable db = ds1.moz_historyvisits;
                //            db.CloseDatabase();
                foreach (DataRow r in ds1.moz_historyvisits.Rows)
                {
                    URL.Add(r["url"].ToString());
                    Title.Add(r["title"].ToString());
                }
                sqLiteConnection1.Close(); 
            }
        }
        void tt_MouseHover(object sender, EventArgs e)
        {
//            RecDoc.Clear();
            
//            System.IO.DirectoryInfo Recdir =
//                new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Recent));
//            foreach (FileInfo file in Recdir.GetFiles())
//            {
//                RecDoc.Add(file.FullName);
//            }
            TempF = (sender as PerPixelAlphaForm);
            var F = TempF.Owner as PerPixelAlphaForm;
            TreeListNode TTTT = null;
            for (int i = 0; i < treeListView1.Nodes.Count; i++)
            {
                if (treeListView1.Nodes[i].SubItems[0].Text == F.Handle.ToString())
                {
                    TTTT = treeListView1.Nodes[i];
                }

            }
            int Pinned = 0;
            try
            {
                Pinned = Convert.ToInt32(TTTT.SubItems[7].Text);
            }
            catch
            {

                Pinned = 0;
            }
            if (TTTT.Nodes.Count > 0)
            {
                IsLeaved = false;

//                if (FHandle != (sender as Form).Owner.Handle)
//                {
//                    if (Pop2 != null)
//                    {
//                        Pop2.Dispose();
//                        Pop2 = null;
//                    }
//                    if (RightClickForm != null)
//                    {
//                        if (RightClickForm != null)
//                        {
//                            RightClickForm.Dispose();
//                        }
//                    }
//                    TNew = new Timer();
//                    TNew.Tick += TNew_Tick;
//                    TNew.Tag = (sender as Form);
//                    TNew.Interval = 1000;
//                    TNew.Enabled = true;
//                    TNew.Start();
//                }
//                FHandle = (sender as Form).Owner.Handle;

                string S = "";
                if (F.Tag != null)
                {
                    S = F.Tag.ToString();
                }
                
                if (Path.GetFileNameWithoutExtension(TTTT.Text).ToLower() == "firefox")
                {
                    GetFirefoxHistory();
                }
                if ((F != T) & (S != "Flashing"))
                {
                    TreeListNode TTT = null;
                    for (int i = 0; i < treeListView1.Nodes.Count; i++)
                    {
                        if (treeListView1.Nodes[i].SubItems[0].Text == F.Handle.ToString())
                        {
                            TTT = treeListView1.Nodes[i];
                        }

                    }
                    Bitmap ButtonHoverBmp = new Bitmap(ButtonHoverBmpS);
                    Bitmap ButtonHoverBmp2 = new Bitmap(ButtonHoverBmpS2);
                    Bitmap ButtonHoverBmp3 = new Bitmap(ButtonHoverBmpS3);
                    if (TTT.Nodes.Count == 2)
                    {
                        F.SetBitmap(ButtonHoverBmp2);
                    }
                    else if (TTT.Nodes.Count > 2)
                    {
                        F.SetBitmap(ButtonHoverBmp3);
                    }
                    else
                    {
                        F.SetBitmap(ButtonHoverBmp);
                    }
                    ButtonHoverBmp2.Dispose();
                    ButtonHoverBmp3.Dispose();
                    //               F.SetBitmap(ButtonHoverBmp); 
                    ButtonHoverBmp.Dispose();
                    ButtonHoverBmp = null;
                    GC.Collect();
                } 
            }
            else
            {
                if (Path.GetFileNameWithoutExtension(TTTT.Text).ToLower() == "firefox")
                {
                    GetFirefoxHistory();
                }
                Bitmap QLHover = new Bitmap(ButtonQLH);
                F.SetBitmap(QLHover);
                QLHover.Dispose();
            }

            
        }

        public void FlushMemory()
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                WindowsAPI.SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
            }
        }

          void TNew_Tick(object sender, EventArgs e)
        {
            int AllighYL = 0;
            try
            {
                AllighYL = Convert.ToInt32(Skin.IniReadValue("TASKBARLEFTCLICK", "YAlligment"));
            }
            catch
            {

                MessageBox.Show("Invalid Skin");

            }
            TreeListNode TTT = null;
            foreach (TreeListNode tln in treeListView1.Nodes)
            {
                if (tln.SubItems[0].Text == (((((sender as Timer).Tag) as PerPixelAlphaForm).Owner) as PerPixelAlphaForm).Handle.ToString())
                {
                    TTT = tln; 
                } 
            }
            int MaxSize = Convert.ToInt32(SettingsIni.IniReadValue("Main", "TrackBarThu"));
            float Dev = Screen.PrimaryScreen.WorkingArea.Width/(MaxSize);
            if (!IsLeaved)
            {
                if (Pop2 == null & Pop == null)
                {

                    if (!ThuDisable)
                    {
                        if (TTT.Nodes.Count < 10)
                        {
                            LoadPop(((sender as Timer).Tag) as Form);  
                        }
                        else
                        {
                            LoadLeftClickMenu(((((sender as Timer).Tag) as PerPixelAlphaForm).Owner) as PerPixelAlphaForm, AllighYL);
                        }
                    }
                    else
                    {
                        LoadLeftClickMenu(((((sender as Timer).Tag) as PerPixelAlphaForm).Owner) as PerPixelAlphaForm, AllighYL);
                    }

                }
//                else
//                {
//                    PopUP.Dispose();
//                    LoadPop(((sender as Timer).Tag) as Form);
//                } 
            }
//            FlushMemory();
            (sender as Timer).Stop();
            (sender as Timer).Dispose();
        }

        public void UpdateDesktop()
        {
            var r = new Rectangle(0, 0, 1280, 1024);
            WindowsAPI.InvalidateRect(IntPtr.Zero, ref r, true);
        }

        public class Thumbnails
        {
            public IntPtr Handle = IntPtr.Zero;

            public void ShowThumbnails()
            {
            }

        }

          void PopUpWindow_Load(object sender, EventArgs e)
        {

        }

	    private void FO_MouseHoverC(object sender, EventArgs e)
	    {
            if (Tim22 != null)
            {
                Tim22.Stop();
            }
        }
        #region AnimationINFO
        [StructLayout(LayoutKind.Sequential)]
        private struct ANIMATIONINFO
        {

            public ANIMATIONINFO(bool iMinAnimate)
            {
                cbSize = GetSize();
                if (iMinAnimate) this.iMinAnimate = 1;
                else this.iMinAnimate = 0;
            }

            public uint cbSize;
            private int iMinAnimate;

            public bool IMinAnimate
            {
                get
                {
                    if (iMinAnimate == 0) return false;
                    else return true;
                }
                set
                {
                    if (value) iMinAnimate = 1;
                    else iMinAnimate = 0;
                }
            }

            public static uint GetSize()
            {
                return (uint)Marshal.SizeOf(typeof(ANIMATIONINFO));
            }
        }

        [DllImport("user32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool SystemParametersInfo(WindowsAPI.SPI uiAction, uint uiParam,
         ref ANIMATIONINFO pvParam, WindowsAPI.SPIF fWinIni);

        public   void SetMinimizeMaximizeAnimation(bool status)
        {
            var animationInfo = new ANIMATIONINFO(status);
            SystemParametersInfo(WindowsAPI.SPI.SPI_GETANIMATION, ANIMATIONINFO.GetSize(),
             ref animationInfo, WindowsAPI.SPIF.None);

            if (animationInfo.IMinAnimate != status)
            {
                animationInfo.IMinAnimate = status;
                SystemParametersInfo(WindowsAPI.SPI.SPI_SETANIMATION, ANIMATIONINFO.GetSize(),
                 ref animationInfo, WindowsAPI.SPIF.SPIF_SENDCHANGE);
            }
        } 
        #endregion

	    private void FO_MouseLeaveW(object sender, EventArgs e)
	    {
            if ((MousePosition.X < (((sender as PerPixelAlphaForm).OwnedForms[0]) as PerPixelAlphaForm).Location.X) |
                (MousePosition.Y < (((sender as PerPixelAlphaForm).OwnedForms[0]) as PerPixelAlphaForm).Location.Y) |
                (MousePosition.Y > (((sender as PerPixelAlphaForm).OwnedForms[0]) as PerPixelAlphaForm).Location.Y + 16)|
                (MousePosition.X > (((sender as PerPixelAlphaForm).OwnedForms[0]) as PerPixelAlphaForm).Location.X + 16))
            {

                    WindowsAPI.ShowWindow(
                        (int) (((sender as PerPixelAlphaForm).OwnedForms[0]) as PerPixelAlphaForm).Handle, 0);
                    Bitmap Temp = new Bitmap(HoverBgrThu);
                    Bitmap Real = CreateTiledImage(
                        (((sender as PerPixelAlphaForm).Owner).OwnedForms[0] as PerPixelAlphaForm).Width,
                        (((sender as PerPixelAlphaForm).Owner).OwnedForms[0] as PerPixelAlphaForm).Height, Temp, false);
                    (((sender as PerPixelAlphaForm).Owner) as PerPixelAlphaForm).SetBitmap(Real,0);
                    Real.Dispose();
                    Temp.Dispose();

            }
	        if (Tim22 != null)
            {
                Tim22.Start();
            }

	    }

	    private void FO_MouseHoverW(object sender, EventArgs e)
	    {
            WindowsAPI.ShowWindow((int)(((sender as PerPixelAlphaForm).OwnedForms[0]) as PerPixelAlphaForm).Handle, 1);
            Bitmap Temp = new Bitmap(HoverBgrThu);
	        Bitmap Real = CreateTiledImage(
	            (((sender as PerPixelAlphaForm).Owner).OwnedForms[0] as PerPixelAlphaForm).Width,
	            (((sender as PerPixelAlphaForm).Owner).OwnedForms[0] as PerPixelAlphaForm).Height, Temp, false);
            (((sender as PerPixelAlphaForm).Owner) as PerPixelAlphaForm).SetBitmap(Real);
            Real.Dispose();
            Temp.Dispose();
            if (Tim22 != null)
            {
                Tim22.Stop();
            }

	    }

	    void FO_Closed(object sender, EventArgs e)
        {
//            (sender as Form).Dispose();
//	        (sender as Form) = null;
	        Pop2 = null;
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
        }

         void PopUpWindowClose_Click(object sender, EventArgs e)
        {
             var Temp = sender as PerPixelAlphaForm;
             var winh = (IntPtr)Convert.ToInt32(Temp.Tag);
             string WinName = WindowsAPI.GetWindowModuleFileName(winh);
             
             TreeListNode T1 = treeListView1.GetItemAt(Path.GetFileName(WinName));
             int PosH = treeListView1.Nodes.IndexOf(T1);
             WindowsAPI.PostMessage(winh, WindowsAPI.WM_SYSCOMMAND, (IntPtr)WindowsAPI.SC_CLOSE, IntPtr.Zero);
             for (int i = 0; i < T1.Nodes.Count; i++)
             {
                 if (T1.Nodes[i].SubItems[2].Text == winh.ToString())
                 {
                     try
                     {
                         T1.Nodes[i].Remove();
                     }
                     catch
                     {
                         
                     }
                 } 
             }
             try
             {
//                 MessageBox.Show(T1.SubItems[0].Text + "    Count:" + T1.Nodes.Count.ToString());
                 PerPixelAlphaForm F = FromHandle((IntPtr) Convert.ToInt32(T1.SubItems[0].Text)) as PerPixelAlphaForm;
                 int Pined = 0;
                 try
                 {
                     Pined = Convert.ToInt32(T1.SubItems[7].Text);
                 }
                 catch
                 {

                     Pined = 0;
                 }
                 if (T1.Nodes.Count == 0)
                 {

                     if (Pined != 1)
                     {
                         F.Dispose();
                         for (int i = PosH; i < treeListView1.Nodes.Count - 1; i++)
                         {
                             Form MF;
                             Form MIF;
                             Form MC;

                             MF = FromHandle((IntPtr)Convert.ToInt32(treeListView1.Nodes[i + 1].SubItems[0].Text)) as Form;

                             MF.Location = new Point(MF.Location.X - MF.Width - 2, MF.Location.Y);

                             MIF = FromHandle((IntPtr)Convert.ToInt32(treeListView1.Nodes[i + 1].SubItems[1].Text)) as Form;

                             MIF.Location = new Point(MIF.Location.X - MF.Width - 2, MIF.Location.Y);

                             MC = FromHandle((IntPtr)Convert.ToInt32(treeListView1.Nodes[i + 1].SubItems[5].Text)) as Form;

                             MC.Location = new Point(MC.Location.X - MF.Width - 2, MC.Location.Y);
                         }
                         T1.Remove(); 
                     }
                     else
                     {
                         Bitmap ButtonBmpT = new Bitmap(ButtonBmpS);
                         Bitmap ButtonBmp = new Bitmap(TransS);
                         Bitmap QL = new Bitmap(ButtonBmp, ButtonBmpT.Width, ButtonBmpT.Height);
                         ButtonBmpT.Dispose();
                         ButtonBmp.Dispose();
                         F.SetBitmap(QL);

                     }
                 }
             }
             catch
             {

             }
             if (Pop2 != null)
             {
                 Pop2.Dispose();
                 Pop2 = null; 
             }


        }

          void PopUpWindow_Click(object sender, EventArgs e)
        {
            var Temp = sender as PerPixelAlphaForm;
            var winh = (IntPtr)Convert.ToInt32(Temp.Tag);
            if ((WindowsAPI.GetWindowLong(winh, WindowsAPI.GWL_STYLE) & WindowsAPI.WS_MINIMIZE) != 0)
            {
                WindowsAPI.SetForegroundWindow(winh);
                Foreground = winh;
                WindowsAPI.ShowWindow((int)winh, (int)WindowsAPI.SW.RESTORE);
            }
            else
            {
                if (Foreground != winh)
                {
                    WindowsAPI.SetForegroundWindow(winh);
                    Foreground = winh;
                }
                else
                {
                    WindowsAPI.ShowWindow((int)winh, (int)WindowsAPI.SW.MINIMIZE);
                }
            }
        }

          void FO_MouseLeave(object sender, EventArgs e)
        {

        }

        void FO_MouseHover(object sender, EventArgs e)
        {
            if (Tim22 != null)
            {
                Tim22.Stop();
            }
        }

        void FO_Load(object sender, EventArgs e)
        {
            if (IsLeaved)
            {
                (sender as Form).Dispose();
            }
//            Tim22 = new WaitableTimer(1000);
//            Tim22.ResumeSuspended = false;
//            Tim22.Elapsed += Tim22_Elapsed;
//            Tim22.Stop();
            FO = (sender as PerPixelAlphaForm);
        }

        void Tim22_Elapsed(object sender, WaitableTimer.ElapsedEventArgs e)
        {
            FO.Dispose();
            Tim22.Stop();
        }

	      void t_MouseLeave(object sender, EventArgs e)
        {
	        PerPixelAlphaForm Frm = (sender as PerPixelAlphaForm);
            Rectangle RForm = new Rectangle(Frm.Location.X, Frm.Location.Y, Frm.Width, Frm.Height);
            if (!RForm.Contains(MousePosition))
            {
                IsLeaved = true;
                FHandle = IntPtr.Zero;
                //            if (FHandle != (sender as Form).Handle)
                //	        {
                //	            FHandle = (sender as Form).Handle; 
                //	        }
                //            else
                //            {

                //            }
                //	        if (PForm != null)
                //	        {
                //                Timer t = new Timer();
                //	            t.Interval = 1000;
                //	            t.Enabled = true;
                //	            t.Tag = (sender as Form);
                //                t.Tick += new EventHandler(t_Tick);
                //                t.Start();
                ////                Rectangle r = new Rectangle(PForm.Location.X,PForm.Location.Y,PForm.Width,PForm.Height);
                ////                Rectangle r2 = new Rectangle((PForm.Tag as Form).Location.X, (PForm.Tag as Form).Location.Y, (PForm.Tag as Form).Width, (PForm.Tag as Form).Height);
                ////	            if (!r.Contains(MousePosition) & !r2.Contains(MousePosition))
                ////	            {
                ////	                PForm.Dispose();
                ////	            }
                //	        }
                var F = (sender as PerPixelAlphaForm);
                TreeListNode TTT = null;
                for (int i = 0; i < treeListView1.Nodes.Count; i++)
                {
                    if (treeListView1.Nodes[i].SubItems[0].Text == F.Handle.ToString())
                    {
                        TTT = treeListView1.Nodes[i];
                    }

                }
                string S = "";
                if (F.Tag != null)
                {
                    S = F.Tag.ToString();
                }
                if (TTT.Nodes.Count > 0)
                {
                    if (F != T & S != "Flashing")
                    {
                        Bitmap BtnBMP = new Bitmap(ButtonBmpS);
                        Bitmap ButtonBmp2 = new Bitmap(ButtonBmpS2);
                        Bitmap ButtonBmp3 = new Bitmap(ButtonBmpS3);
                        if (TTT.Nodes.Count == 2)
                        {
                            F.SetBitmap(ButtonBmp2);
                        }
                        else if (TTT.Nodes.Count > 2)
                        {
                            F.SetBitmap(ButtonBmp3);
                        }
                        else if (TTT.Nodes.Count > 0)
                        {
                            F.SetBitmap(BtnBMP);
                        }
                        ButtonBmp2.Dispose();
                        ButtonBmp3.Dispose();
                        //                F.SetBitmap(BtnBMP);
                        BtnBMP.Dispose();
                        BtnBMP = null;
                        GC.Collect(7);
                    }
                    //            if (Tim != null)
                    //            {
                    //                Tim.Start();
                    //            }
                    if (FHandle != (sender as Form).Handle)
                    {
                        //                if (PForm != null)
                        //                {
                        //                    PForm.Close();
                        //                    PForm = null;
                        //                }
                        if (Tim22 != null)
                        {
                            Tim22.Start();
                        }
                    }
                }
                else
                {
                    Bitmap ButtonBmpT = new Bitmap(ButtonBmpS);
                    Bitmap ButtonBmp = new Bitmap(TransS);
                    Bitmap QL = new Bitmap(ButtonBmp, ButtonBmpT.Width, ButtonBmpT.Height);
                    ButtonBmpT.Dispose();
                    ButtonBmp.Dispose();
                    F.SetBitmap(QL);
                } 
            }

	        try
	        {
                GC.Collect(10, GCCollectionMode.Forced);
	        }
	        finally
	        {
                GC.Collect(10, GCCollectionMode.Forced); 
	        }


        }

        void t_Tick(object sender, EventArgs e)
        {
            if (RightClickForm != null)
            {
                Rectangle r = new Rectangle(RightClickForm.Location.X, RightClickForm.Location.Y, RightClickForm.Width, RightClickForm.Height);
                if (!r.Contains(MousePosition))
                {
                    RightClickForm.Dispose();
                   (sender as Timer).Stop();
                    (sender as Timer).Dispose();
                }
                else
                {
                   (sender as Timer).Stop();
                   (sender as Timer).Dispose(); 
                }
            }
        }

        void tt_MouseHover_Real(object sender, EventArgs e)
        {
            int IntervalT = Convert.ToInt32(SettingsIni.IniReadValue("Main", "ThuSpeed"));
            if (FHandle != (sender as Form).Owner.Handle)
            {
                if (Pop2 != null)
                {
                    Pop2.Dispose();
                    Pop2 = null;
                }
                if (RightClickForm != null)
                {
                    if (RightClickForm != null)
                    {
                        RightClickForm.Dispose();
                    }
                }
                TNew = new Timer();
                TNew.Tick += TNew_Tick;
                TNew.Tag = (sender as Form);
                TNew.Interval = IntervalT;
                TNew.Enabled = true;
                TNew.Start();
            }
            FHandle = (sender as Form).Owner.Handle;
        }
          void t_MouseHover_Real(object sender, EventArgs e)
        {
            int IntervalT = Convert.ToInt32(SettingsIni.IniReadValue("Main", "ThuSpeed"));
            if (FHandle != (sender as Form).Handle)
            {
                var TNew = new Timer();
                TNew.Tick += TNew_Tick;
                TNew.Tag = (sender as Form).OwnedForms[0];
                TNew.Interval = IntervalT;
                TNew.Enabled = true;
                TNew.Start();
            }
            FHandle = (sender as Form).Handle;
        }
          void t_MouseHover(object sender, EventArgs e)
        {
//            var sv = new OperatingSystemVersion();
            int Scale = Desktop.Top / Desktop.Bottom;

            TempF = (sender as PerPixelAlphaForm);

            IntPtr IPW;
            if (Pop2 != null)
            {
                IPW = (IntPtr)Pop2.Tag;
            }
            else
            {
                IPW = IntPtr.Zero;
            }
            if (FHandle != (sender as Form).Handle)
            {
                if ((Tim22 != null) & (TempF.Handle == IPW))
                {
                    if (Pop2 != null)
                    {
                        Pop2.Dispose();
                        Pop2 = null; 
                    }
                    if (RightClickForm != null)
                    {
                        RightClickForm.Dispose();
                    }
                    Tim22.Stop();
                }
                else
                {

                    if ((Tim22 != null))
                    {
                        if (Pop2!=null)
                        {
                            Pop2.Dispose(); 
                            Pop2 = null;
                        }
                        
                        Tim22.Stop();
                    }
                } 
            }

            IsLeaved = false;
//            if (FHandle != (sender as Form).Handle)
//            {
//                var TNew = new Timer();
//                TNew.Tick += TNew_Tick;
//                TNew.Tag = (sender as Form).OwnedForms[0];
//                TNew.Interval = 1000;
//                TNew.Enabled = true;
//                TNew.Start(); 
//            }
//            FHandle = (sender as Form).Handle;

            IntPtr IP;
            if (PopUP != null)
            {
                IP = (IntPtr)Convert.ToInt32(PopUP.Tag);
            }
            else
            {
                IP = IntPtr.Zero;
            }
//            if ((Tim != null) & ((sender as Form).Handle == IP))
//            {
//                Tim.Stop();
//            }
//            else
//            {
//
//                if ((Tim != null))
//                {
//                    Tim.Start();
//                }
//            }

            var F = (sender as PerPixelAlphaForm);
            TreeListNode TTT = null;
            for (int i = 0; i < TLV.Nodes.Count; i++)
            {
                if (TLV.Nodes[i].SubItems[0].Text == F.Handle.ToString())
                {
                    TTT = TLV.Nodes[i];
                }

            }
            string S = "";
            if (F.Tag != null)
            {
                S = F.Tag.ToString(); 
            }
            int Pinned = 0;
            try
            {
                Pinned = Convert.ToInt32(TTT.SubItems[7].Text);
            }
            catch
            {

                Pinned = 0;
            }

            if (TTT.Nodes.Count > 0)
            {
                if (Path.GetFileNameWithoutExtension(TTT.Text).ToLower() == "firefox")
                {
                    GetFirefoxHistory();
                }
                if ((F != T) & (S != "Flashing"))
                {
                    Bitmap HoverBMP = new Bitmap(ButtonHoverBmpS);
                    Bitmap ButtonHoverBmp2 = new Bitmap(ButtonHoverBmpS2);
                    Bitmap ButtonHoverBmp3 = new Bitmap(ButtonHoverBmpS3);
                    if (TTT.Nodes.Count == 2)
                    {
                        F.SetBitmap(ButtonHoverBmp2);
                    }
                    else if (TTT.Nodes.Count > 2)
                    {
                        F.SetBitmap(ButtonHoverBmp3);
                    }
                    else
                    {
                        F.SetBitmap(HoverBMP);
                    }
                    ButtonHoverBmp2.Dispose();
                    ButtonHoverBmp3.Dispose();
                    //                F.SetBitmap(HoverBMP);
                    HoverBMP.Dispose();
                    HoverBMP = null;
                    GC.Collect(7);
                } 
            }
            else
            {
                if (Path.GetFileNameWithoutExtension(TTT.Text).ToLower() == "firefox")
                {
                    GetFirefoxHistory();
                }
                Bitmap QLHover = new Bitmap(ButtonQLH);
                F.SetBitmap(QLHover);
                QLHover.Dispose();
            }

        }

        private bool Callback(IntPtr hwnd, int lParam)
        {
            if (Handle != hwnd & ((WindowsAPI.GetWindowLongA(hwnd, WindowsAPI.GWL_STYLE) & WindowsAPI.TARGETWINDOW) == WindowsAPI.TARGETWINDOW) &
                (((WindowsAPI.GetWindowLongA(hwnd, WindowsAPI.GWL_EXSTYLE) & WindowsAPI.WS_EX_CONTROLPARENT) != WindowsAPI.WS_EX_CONTROLPARENT) | ((WindowsAPI.GetWindowLongA(hwnd, WindowsAPI.GWL_EXSTYLE) & WindowsAPI.WS_EX_APPWINDOW) == WindowsAPI.WS_EX_APPWINDOW) | ((WindowsAPI.GetWindowLongA(hwnd, WindowsAPI.GWL_STYLE) & WindowsAPI.WS_SYSMENU) == WindowsAPI.WS_SYSMENU)))
//            if (Handle != hwnd)
            {

                if (((WindowsAPI.GetWindowLongA(hwnd, WindowsAPI.GWL_EXSTYLE) & WindowsAPI.WS_EX_TOOLWINDOW) != WindowsAPI.WS_EX_TOOLWINDOW) | ((WindowsAPI.GetWindowLongA(hwnd, WindowsAPI.GWL_EXSTYLE) & WindowsAPI.WS_EX_APPWINDOW) == WindowsAPI.WS_EX_APPWINDOW))
                {
                    if (((WindowsAPI.GetWindowLongA(hwnd, WindowsAPI.GWL_EXSTYLE) & WindowsAPI.WS_EX_NOACTIVATE) != WindowsAPI.WS_EX_NOACTIVATE))
                    {
                        var sb = new StringBuilder(200);
                        WindowsAPI.GetWindowText(hwnd, sb, sb.Capacity);
                        var t = new Window();
                        t.Handle = hwnd;
                        t.Title = sb.ToString();
                        windows.Add(t); 
                    } 
                }
            }

            return true; //continue enumeration
        } 
        #endregion
        
        Thread CleanT = null;//new Thread(new ThreadStart(CleanTaskbar));
	    private   TreeListView TLV = null;
        int O = 0;

        private void FillButtons()
        {
            for (int i = 0; i < treeListView1.Nodes.Count; i++)
            {
                for (int j = 0; j < treeListView1.Nodes[i].Nodes.Count; j++)
                {
                    IntPtr Hwnd = (IntPtr)Convert.ToInt32(treeListView1.Nodes[i].Nodes[j].SubItems[2].Text);
                    PerPixelAlphaForm F =
                        FromHandle((IntPtr)Convert.ToInt32(treeListView1.Nodes[i].SubItems[0].Text)) as
                          PerPixelAlphaForm;

                    if (treeListView1.Nodes[i].Nodes.Count >= 1)
                    {
                        if (treeListView1.Nodes[i].Nodes.Count >= 3)
                        {
                            Bitmap ButtonActive3 = new Bitmap(ButtonPressedBmpS3);
                            Bitmap Button3 = new Bitmap(ButtonBmpS3);
                            Bitmap ButtonFlashB3 = new Bitmap(ButtonFlash3);
                            if (F.Tag == "Flashing")
                            {
                                F.SetBitmap(ButtonFlashB3);
                            }
                            else
                            {
                                F.SetBitmap(Button3);
                            }
                            ButtonActive3.Dispose();
                            Button3.Dispose();
                            ButtonFlashB3.Dispose();
                        }
                        else if (treeListView1.Nodes[i].Nodes.Count == 2)
                        {
                            string ghghg = treeListView1.Nodes[i].Text;
                            Bitmap ButtonActive2 = new Bitmap(ButtonPressedBmpS2);
                            Bitmap Button2 = new Bitmap(ButtonBmpS2);
                            Bitmap ButtonFlashB2 = new Bitmap(ButtonFlash2);

                            if (F.Tag == "Flashing")
                            {
                                F.SetBitmap(ButtonFlashB2);
                            }
                            else
                            {
                                F.SetBitmap(Button2);
                            }
                            ButtonActive2.Dispose();
                            Button2.Dispose();
                            ButtonFlashB2.Dispose();
                        }
                        else if (treeListView1.Nodes[i].Nodes.Count == 1)
                        {
                            Bitmap ButtonActive = new Bitmap(ButtonPressedBmpS);
                            Bitmap Button = new Bitmap(ButtonBmpS);
                            Bitmap ButtonFlashB = new Bitmap(ButtonFlash);

                            if (F.Tag == "Flashing")
                            {
                                F.SetBitmap(ButtonFlashB);
                            }
                            else
                            {
                                F.SetBitmap(Button);
                            }
                            ButtonActive.Dispose();
                            Button.Dispose();
                            ButtonFlashB.Dispose();
                        }

                    }
                    else
                    {
                        Bitmap ButtonPinned = new Bitmap(ButtonQLH);
                        F.SetBitmap(ButtonPinned, 20);
                        ButtonPinned.Dispose();
                    }

                }
            }
        }

        void CleanTaskbar()
        {
            for (int i = 0; i < TLV.Nodes.Count - 1; i++)
            {
                for (int j = 0; j < TLV.Nodes[i].Nodes.Count; j++)
                {
                    IntPtr Hwnd = (IntPtr)Convert.ToInt32(TLV.Nodes[i].Nodes[j].SubItems[2].Text);
                    PerPixelAlphaForm F =
                        FromHandle((IntPtr)Convert.ToInt32(TLV.Nodes[i].SubItems[0].Text)) as
                          PerPixelAlphaForm;
                    bool IsThereActiveWin = false;
                    IntPtr ActiveHwnd = WindowsAPI.GetForegroundWindow();
                    if (!WindowsAPI.IsWindow(Hwnd))
                    {
                        for (int k = 0; k < TLV.Nodes[i].Nodes.Count; k++)
                        {
                            if (TLV.Nodes[i].Nodes[k].SubItems[2].Text == ActiveHwnd.ToString())
                            {
                                IsThereActiveWin = true;  
                            }  
                        }
                        if (TLV.Nodes[i].Nodes.Count > 1)
                        {
                            if (TLV.Nodes[i].Nodes.Count - 1 >= 3)
                            {
                                Bitmap ButtonActive3 = new Bitmap(ButtonPressedBmpS3);
                                Bitmap Button3 = new Bitmap(ButtonBmpS3);
                                Bitmap ButtonFlashB3 = new Bitmap(ButtonFlash3);
                                if (IsThereActiveWin)
                                {
                                    F.SetBitmap(ButtonActive3); 
                                }
                                else if (F.Tag == "Flashing")
                                {
                                    F.SetBitmap(ButtonFlashB3);
                                }
                                else
                                {
                                    F.SetBitmap(Button3); 
                                }
                                ButtonActive3.Dispose();
                                Button3.Dispose();
                                ButtonFlashB3.Dispose();
                            }
                            else if (TLV.Nodes[i].Nodes.Count - 1 == 2)
                            {
                                Bitmap ButtonActive2 = new Bitmap(ButtonPressedBmpS2);
                                Bitmap Button2 = new Bitmap(ButtonBmpS2);
                                Bitmap ButtonFlashB2 = new Bitmap(ButtonFlash2);
                         
                                if (IsThereActiveWin)
                                {
                                    F.SetBitmap(ButtonActive2); 
                                }
                                else if (F.Tag == "Flashing")
                                {
                                    F.SetBitmap(ButtonFlashB2);
                                }
                                else
                                {
                                    F.SetBitmap(Button2); 
                                }
                                ButtonActive2.Dispose();
                                Button2.Dispose();
                                ButtonFlashB2.Dispose();
                            }
                            else if (TLV.Nodes[i].Nodes.Count - 1 == 1)
                            {
                                Bitmap ButtonActive = new Bitmap(ButtonPressedBmpS);
                                Bitmap Button = new Bitmap(ButtonBmpS);
                                Bitmap ButtonFlashB = new Bitmap(ButtonFlash);
                                
                                if (IsThereActiveWin)
                                {
                                    F.SetBitmap(ButtonActive);
                                }
                                else if (F.Tag == "Flashing")
                                {
                                    F.SetBitmap(ButtonFlashB);
                                }
                                else
                                {
                                    F.SetBitmap(Button);
                                }
                                ButtonActive.Dispose();
                                Button.Dispose();
                                ButtonFlashB.Dispose();  
                            }
                            TLV.Nodes[i].Nodes.RemoveAt(j);
                        }
                        else
                        {
                                int POSH = -1;
                                for (int ii = 0; ii < treeListView1.Nodes.Count; ii++)
                                {

                                    if (treeListView1.Nodes[ii].SubItems[0].Text == F.Handle.ToString())
                                    {

                                        POSH = treeListView1.Nodes.IndexOf(treeListView1.Nodes[ii]);

                                    }

                                }
                                int Pined = 0;
                                try
                                {
                                    Pined = Convert.ToInt32(treeListView1.Nodes[POSH].SubItems[7].Text);
                                }
                                catch
                                {

                                    Pined = 0;
                                }
                                if (Pined != 1)
                                {

                                    try
                                    {
                                        F.OwnedForms[0].OwnedForms[0].Dispose();
                                    }
                                    catch
                                    {

                                    }
                                    try
                                    {
                                        F.OwnedForms[0].Dispose();
                                    }
                                    catch
                                    {

                                    }
                                    F.Close();
                                    for (int jj = POSH; jj < treeListView1.Nodes.Count - 1; jj++)
                                    {
                                        Form MF;
                                        Form MIF;
                                        Form MC;

                                        MF = FromHandle((IntPtr)Convert.ToInt32(treeListView1.Nodes[jj + 1].SubItems[0].Text)) as Form;

                                        MF.Location = new Point(MF.Location.X - MF.Width - 2, MF.Location.Y);

                                        MIF = FromHandle((IntPtr)Convert.ToInt32(treeListView1.Nodes[jj + 1].SubItems[1].Text)) as Form;

                                        MIF.Location = new Point(MIF.Location.X - MF.Width - 2, MIF.Location.Y);

                                        try
                                        {
                                            MC = FromHandle((IntPtr)Convert.ToInt32(treeListView1.Nodes[jj + 1].SubItems[5].Text)) as Form;

                                            MC.Location = new Point(MC.Location.X - MF.Width - 2, MC.Location.Y);
                                        }
                                        catch
                                        {

                                        }
                                    }

                                    treeListView1.Nodes[POSH].Remove();
                                }
                                else
                                {
                                    Bitmap ButtonPinned = new Bitmap(ButtonQLH);
                                    F.SetBitmap(ButtonPinned, 20);
                                    ButtonPinned.Dispose();
                                }
//                            F.Dispose();//sadasdad
                        }
                    }
                    IsThereActiveWin = false;
                } 
            }
//            O++;
//            Form I = new Form();
//            I.Text = O.ToString();
//            I.ShowDialog();
        }


        void MinMax(IntPtr Handle)
        {
  
            IsPushMinimize = false;

            string s = "";
            try
            {
                s = Path.GetFileName(WindowsAPI.GetWindowModuleFileName(Handle)).ToLower();
            }
            catch
            {
                s = "";
            }
            if (true)//File.Exists(WindowsAPI.GetWindowModuleFileName(Handle)))
            {

                var REC = new WindowsAPI.RECTW();
                WindowsAPI.GetWindowRect(Handle, ref REC);
                TreeListNode TT = null;
                try
                {
                    for (int i = 0; i < treeListView1.Nodes[treeListView1.Nodes.IndexOf(treeListView1.GetItemAt(s))].Nodes.Count; i++)
                    {
                        if (treeListView1.Nodes[treeListView1.Nodes.IndexOf(treeListView1.GetItemAt(s))].Nodes[i].SubItems[2].Text == Handle.ToString())
                        {
                            TT = treeListView1.Nodes[treeListView1.Nodes.IndexOf(treeListView1.GetItemAt(s))].Nodes[i];
                        }
                    }
                }
                catch
                {


                }

                PerPixelAlphaForm PAFT;
                try
                {
                    PAFT = FromHandle((IntPtr)Convert.ToInt32(treeListView1.Nodes[treeListView1.Nodes.IndexOf(treeListView1.GetItemAt(s))].SubItems[0].Text)) as PerPixelAlphaForm;
                    TreeListNode tln = treeListView1.Nodes[treeListView1.Nodes.IndexOf(treeListView1.GetItemAt(s))];
                    bool bb = WindowsAPI.IsIconic(Handle);
                    if (bb)
                    {
                        Bitmap ButtonBmp = new Bitmap(ButtonBmpS);
                        Bitmap ButtonBmp2 = new Bitmap(ButtonBmpS2);
                        Bitmap ButtonBmp3 = new Bitmap(ButtonBmpS3);
                        if (tln.Nodes.Count == 2)
                        {
                            PAFT.SetBitmap(ButtonBmp2);
                        }
                        else if (tln.Nodes.Count > 2)
                        {
                            PAFT.SetBitmap(ButtonBmp3);
                        }
                        else if (tln.Nodes.Count > 0)
                        {
                            PAFT.SetBitmap(ButtonBmp);
                        }
                        ButtonBmp2.Dispose();
                        ButtonBmp3.Dispose();
                        //                    PAFT.SetBitmap(ButtonBmp);
                        ButtonBmp.Dispose();
                        ButtonBmp = null;
                        TT.SubItems[4].Text = "0";
                        TT.SubItems[3].Text = "0";
                        if (REC.Width > 161)
                        {
                            TT.SubItems[6].Text = REC.Width.ToString();
                            TT.SubItems[7].Text = REC.Height.ToString();
                        }



                    }
                    else
                    {
                        //                    Bitmap ButtonPressedBmp = new Bitmap(ButtonPressedBmpS);
                        Bitmap ButtonPressedBMP2 = new Bitmap(ButtonPressedBmpS2);
                        Bitmap ButtonPressedBMP3 = new Bitmap(ButtonPressedBmpS3);
                        Bitmap ButtonPressedBmp = new Bitmap(ButtonPressedBmpS);
                        if (tln.Nodes.Count == 2)
                        {
                            PAFT.SetBitmap(ButtonPressedBMP2);
                        }
                        else if (tln.Nodes.Count > 2)
                        {
                            PAFT.SetBitmap(ButtonPressedBMP3);
                        }
                        else
                        {
                            PAFT.SetBitmap(ButtonPressedBmp);
                        }
                        ButtonPressedBMP2.Dispose();
                        ButtonPressedBMP3.Dispose();
                        //                    PAFT.SetBitmap(ButtonPressedBmp);
                        ButtonPressedBmp.Dispose();
                        ButtonPressedBmp = null;
                        GC.Collect();
                        if (REC.Width > 161)
                        {
                            TT.SubItems[6].Text = REC.Width.ToString();
                            TT.SubItems[7].Text = REC.Height.ToString();
                        }
                    }
                }
                catch
                {


                } 
            }
//            Startbar.TopMost = true;
//            foreach (PerPixelAlphaForm win in Startbar.OwnedForms)
//            {
//                win.TopMost = true;
//            }
        }

        #region Designer and Main functions
		public frmSbarDevelopper()
		{
            
		    IntPtr procHandle = WindowsAPI.GetCurrentProcess();
		    WindowsAPI.SetPriorityClass(procHandle, (uint)WindowsAPI.DWPriorityClass.HIGH_PRIORITY_CLASS);
			InitializeComponent();
            WindowsAPI.ShowWindow((int)Handle, (int)WindowsAPI.SW.HIDE);

            uMsgNotify = WindowsAPI.RegisterWindowMessageA("SHELLHOOK");

            // This basically registers our window to receive the shell events
            Microsoft.Win32.SystemEvents.SessionSwitch += new SessionSwitchEventHandler(SystemEvents_SessionSwitch);
            if (!WindowsAPI.RegisterShellHookWindow(this.Handle))
                throw new Win32Exception(Marshal.GetLastWin32Error());
			// Instantiate our GlobalHooks objec
            if (sv.OSVersion == OSVersion.Vista)
            {
                Timer StartPowerHook = new Timer();
                StartPowerHook.Interval = 30000;
                StartPowerHook.Enabled = true;
                StartPowerHook.Tick += new EventHandler(StartPowerHook_Tick);
                StartPowerHook.Start();
//                RegisterForPowerNotifications(this.Handle); 
            }

		}

        void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
        {
//          
//            e.Reason = 
//            MessageBox.Show(Startbar.OwnedForms.Length.ToString());
            if (true)//e.Reason == SessionSwitchReason.SessionUnlock |e.Reason == SessionSwitchReason.SessionLogon)
	        {
		        Bitmap ButtonBmp = new Bitmap(ButtonBmpS);
                Bitmap ButtonBmp2 = new Bitmap(ButtonBmpS2);
                Bitmap ButtonBmp3 = new Bitmap(ButtonBmpS3);
                Bitmap ButtonPinned = new Bitmap(ButtonQLH);
                Bitmap DesktopSWTemp = new Bitmap(ButtonShowDesktopS);
                WindowsAPI.RECTW TaskPos = new WindowsAPI.RECTW();
                WindowsAPI.GetWindowRect(hReBar, ref TaskPos);
                Bitmap DesktopSW = new Bitmap(DesktopSWTemp, DesktopSWTemp.Width,TaskPos.Height);
	            foreach (Form FForm in Startbar.OwnedForms)
	            {
	                if (FForm.Name == "ShowDesktop")
	                {
                        (FForm as PerPixelAlphaForm).SetBitmap(DesktopSW);
	                }
                    if (FForm.Name == "DropForm")
	                {
                        Bitmap GHJ = new Bitmap(StartBarBackgroubd, TaskPos.Width - 150, TaskPos.Height * 2);
                        (FForm as PerPixelAlphaForm).SetBitmap(GHJ,25); 
                        GHJ.Dispose();
	                }
	            }
                DesktopSW.Dispose();
                for (int i = 0; i < treeListView1.Nodes.Count; i++)
                {
                    Icon II = null;
                    try
                    {
                        if (ButtonBmp.Width > 54)
                        {
                            if (!File.Exists(ExePath + "\\Skins\\" + SkinDir + "\\" + Path.GetFileNameWithoutExtension(treeListView1.Nodes[i].SubItems[6].Text) + ".png"))
                            {
                                II = lst.Icon(lst.IconIndex(treeListView1.Nodes[i].SubItems[6].Text));
                            }
                            else
                            {
                                Bitmap IconBMP = new Bitmap(ExePath + "\\Skins\\" + SkinDir + "\\" + Path.GetFileNameWithoutExtension(treeListView1.Nodes[i].SubItems[6].Text) + ".png");
                                II = BitmapToIcon(IconBMP);
                                IconBMP.Dispose();
                            }
                        }
                        else
                        {
                            if (!File.Exists(ExePath + "\\Skins\\" + SkinDir + "\\" + Path.GetFileNameWithoutExtension(treeListView1.Nodes[i].SubItems[6].Text) + ".png"))
                            {
                                II = lsts.Icon(lsts.IconIndex(treeListView1.Nodes[i].SubItems[6].Text));
                            }
                            else
                            {
                                Bitmap IconBMP = new Bitmap(ExePath + "\\Skins\\" + SkinDir + "\\" + Path.GetFileNameWithoutExtension(treeListView1.Nodes[i].SubItems[6].Text) + ".png");
                                II = BitmapToIcon(IconBMP);
                                IconBMP.Dispose();
                            }
                        }
    //                    II = lsts.Icon(lsts.IconIndex(treeListView1.Nodes[i].SubItems[6].Text));
                    }
                    catch
                    {

                    }
                    PerPixelAlphaForm FPForm =
                            FromHandle((IntPtr)Convert.ToInt32(treeListView1.Nodes[i].SubItems[0].Text)) as
                            PerPixelAlphaForm;
                    if (II != null & Path.GetFileNameWithoutExtension(treeListView1.Nodes[i].SubItems[6].Text).ToLower() != "explorer")
                    {

                        (FPForm.OwnedForms[0] as PerPixelAlphaForm).SetBitmap(II.ToBitmap()); 
                    }
                    else if (Path.GetFileNameWithoutExtension(treeListView1.Nodes[i].SubItems[6].Text).ToLower() == "explorer")
                    {
                        Bitmap ExplorerBM = new Bitmap(ExePath + "\\Skins\\" + SkinDir + "\\explorer32.png");
                        (FPForm.OwnedForms[0] as PerPixelAlphaForm).SetBitmap(ExplorerBM);  
//                            Explorer.Dispose();
                    }
                    
                    if (treeListView1.Nodes[i].Nodes.Count >= 3)
                    {
                        FPForm.SetBitmap(ButtonBmp3);
                    }
                    else if (treeListView1.Nodes[i].Nodes.Count == 2)
                    {
                        FPForm.SetBitmap(ButtonBmp2);  
                    }
                    else if (treeListView1.Nodes[i].Nodes.Count == 1)
                    {
                        FPForm.SetBitmap(ButtonBmp);
                    }
                    else if (treeListView1.Nodes[i].Nodes.Count < 1)
                    {
                        FPForm.SetBitmap(ButtonPinned, 10);
                    }
                    
                }
    //            foreach (Form form in Startbar.OwnedForms)
    //            {
    //                WindowsAPI.SetWindowPos(form.Handle, WindowsAPI.HWND_TOPMOST, 0, 0, 0, 0,
    //                                        WindowsAPI.SWP_NOMOVE | WindowsAPI.SWP_NOSIZE | WindowsAPI.SWP_NOACTIVATE);
    //            }
                Startbar.TopMost = true;
                ButtonBmp.Dispose();
                ButtonBmp2.Dispose();
                ButtonBmp3.Dispose();
                ButtonPinned.Dispose(); 
        }
        }

        void StartPowerHook_Tick(object sender, EventArgs e)
        {
//            IntPtr procHandle = WindowsAPI.GetCurrentProcess();
//            RegisterForPowerNotifications(this.Handle);
//            WindowsAPI.SetPriorityClass(procHandle, (uint)WindowsAPI.DWPriorityClass.NORMAL_PRIORITY_CLASS);
            (sender as Timer).Stop();
            (sender as Timer).Dispose();
        }



        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (sv.OSVersion == OSVersion.Vista)
                {
                    UnregisterForPowerNotifications(); 
                }
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmSbarDevelopper));
            this.icSystray = new System.Windows.Forms.NotifyIcon(this.components);
            this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.optionsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.developperToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.aboutToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.miUpdateCheck = new System.Windows.Forms.ToolStripMenuItem();
            this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.button1 = new System.Windows.Forms.Button();
            this.button3 = new System.Windows.Forms.Button();
            this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.button2 = new System.Windows.Forms.Button();
            this.FlushMemmory = new System.Windows.Forms.Timer(this.components);
            this.timer2 = new System.Windows.Forms.Timer(this.components);
            this.timer3 = new System.Windows.Forms.Timer(this.components);
            this.timer4 = new System.Windows.Forms.Timer(this.components);
            this.ThumbFormClose = new System.Windows.Forms.Timer(this.components);
            this.UpdateTimer = new System.Windows.Forms.Timer(this.components);
            this.sqLiteConnection1 = new System.Data.SQLite.SQLiteConnection();
            this.sqLiteDataAdapter1 = new System.Data.SQLite.SQLiteDataAdapter();
            this.sqliteSelectCommand1 = new System.Data.SQLite.SQLiteCommand();
            this.listView1 = new System.Windows.Forms.ListView();
            this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
            this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
            this.listViewExSort1 = new RR.Windows.Forms.ListViewExSort(this.components);
            this.trPop = new System.Windows.Forms.Timer(this.components);
            this.csSbar = new System.Data.OleDb.OleDbConnection();
            this.ds1 = new GlobalHooksTest.DS();
            this.recentDocBindingSource = new System.Windows.Forms.BindingSource(this.components);
            this.bindingSource1 = new System.Windows.Forms.BindingSource(this.components);
            this.recentTableAdapter = new GlobalHooksTest.DSTableAdapters.RecentTableAdapter();
            this.bsTaskabrPinned = new System.Windows.Forms.BindingSource(this.components);
            this.taskbarPinnedTableAdapter = new GlobalHooksTest.DSTableAdapters.TaskbarPinnedTableAdapter();
            this.pinnedTableAdapter1 = new GlobalHooksTest.DSTableAdapters.PinnedTableAdapter();
            this.tmDrop = new System.Windows.Forms.Timer(this.components);
            this.tmDropP = new System.Windows.Forms.Timer(this.components);
            this.tmAutoHide = new System.Windows.Forms.Timer(this.components);
            this.tmrClean = new System.Windows.Forms.Timer(this.components);
            this.mnuTaskbarRightClick = new System.Windows.Forms.ContextMenu();
            this.miCascade = new System.Windows.Forms.MenuItem();
            this.miStackedWin = new System.Windows.Forms.MenuItem();
            this.miSidebySideWin = new System.Windows.Forms.MenuItem();
            this.menuItem6 = new System.Windows.Forms.MenuItem();
            this.menuItem2 = new System.Windows.Forms.MenuItem();
            this.miTaskmanager = new System.Windows.Forms.MenuItem();
            this.menuItem3 = new System.Windows.Forms.MenuItem();
            this.miLockTaskbar = new System.Windows.Forms.MenuItem();
            this.menuItem5 = new System.Windows.Forms.MenuItem();
            this.exclusionTableAdapter1 = new GlobalHooksTest.DSTableAdapters.ExclusionTableAdapter();
            this.tmRefreshDropZone = new System.Windows.Forms.Timer(this.components);
            this.tmFullScreenDetect = new System.Windows.Forms.Timer(this.components);
            this.contextMenuStrip1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.ds1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.recentDocBindingSource)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.bindingSource1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.bsTaskabrPinned)).BeginInit();
            this.SuspendLayout();
            // 
            // icSystray
            // 
            this.icSystray.BalloonTipIcon = System.Windows.Forms.ToolTipIcon.Info;
            this.icSystray.BalloonTipText = "CreateSbar";
            this.icSystray.BalloonTipTitle = "SBAR";
            this.icSystray.ContextMenuStrip = this.contextMenuStrip1;
            this.icSystray.Icon = ((System.Drawing.Icon)(resources.GetObject("icSystray.Icon")));
            this.icSystray.Text = "SBar Application";
            this.icSystray.Visible = true;
            // 
            // contextMenuStrip1
            // 
            this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.optionsToolStripMenuItem,
            this.developperToolStripMenuItem,
            this.aboutToolStripMenuItem,
            this.miUpdateCheck,
            this.exitToolStripMenuItem});
            this.contextMenuStrip1.Name = "contextMenuStrip1";
            this.contextMenuStrip1.Size = new System.Drawing.Size(166, 114);
            this.contextMenuStrip1.Opened += new System.EventHandler(this.contextMenuStrip1_Opened);
            this.contextMenuStrip1.Opening += new System.ComponentModel.CancelEventHandler(this.contextMenuStrip1_Opening);
            // 
            // optionsToolStripMenuItem
            // 
            this.optionsToolStripMenuItem.Name = "optionsToolStripMenuItem";
            this.optionsToolStripMenuItem.Size = new System.Drawing.Size(165, 22);
            this.optionsToolStripMenuItem.Text = "Options";
            this.optionsToolStripMenuItem.Click += new System.EventHandler(this.optionsToolStripMenuItem_Click);
            // 
            // developperToolStripMenuItem
            // 
            this.developperToolStripMenuItem.Name = "developperToolStripMenuItem";
            this.developperToolStripMenuItem.Size = new System.Drawing.Size(165, 22);
            this.developperToolStripMenuItem.Text = "Developper";
            this.developperToolStripMenuItem.Visible = false;
            this.developperToolStripMenuItem.Click += new System.EventHandler(this.developperToolStripMenuItem_Click);
            // 
            // aboutToolStripMenuItem
            // 
            this.aboutToolStripMenuItem.Name = "aboutToolStripMenuItem";
            this.aboutToolStripMenuItem.Size = new System.Drawing.Size(165, 22);
            this.aboutToolStripMenuItem.Text = "About";
            this.aboutToolStripMenuItem.Click += new System.EventHandler(this.aboutToolStripMenuItem_Click);
            // 
            // miUpdateCheck
            // 
            this.miUpdateCheck.Name = "miUpdateCheck";
            this.miUpdateCheck.Size = new System.Drawing.Size(165, 22);
            this.miUpdateCheck.Text = "Check for update";
            this.miUpdateCheck.Click += new System.EventHandler(this.checkForUpdateToolStripMenuItem_Click);
            // 
            // exitToolStripMenuItem
            // 
            this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
            this.exitToolStripMenuItem.Size = new System.Drawing.Size(165, 22);
            this.exitToolStripMenuItem.Text = "Exit";
            this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click);
            // 
            // button1
            // 
            this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.button1.Location = new System.Drawing.Point(455, 199);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 5;
            this.button1.Text = "Hide";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // button3
            // 
            this.button3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.button3.Location = new System.Drawing.Point(374, 199);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(75, 23);
            this.button3.TabIndex = 7;
            this.button3.Text = "Exit";
            this.button3.UseVisualStyleBackColor = true;
            this.button3.Click += new System.EventHandler(this.button3_Click);
            // 
            // toolStripMenuItem1
            // 
            this.toolStripMenuItem1.Name = "toolStripMenuItem1";
            this.toolStripMenuItem1.Size = new System.Drawing.Size(92, 22);
            this.toolStripMenuItem1.Text = "Exit";
            this.toolStripMenuItem1.Click += new System.EventHandler(this.toolStripMenuItem1_Click);
            // 
            // timer1
            // 
            this.timer1.Enabled = true;
            this.timer1.Interval = 1000;
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            // 
            // button2
            // 
            this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.button2.Location = new System.Drawing.Point(165, 198);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 10;
            this.button2.Text = "button2";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // FlushMemmory
            // 
            this.FlushMemmory.Enabled = true;
            this.FlushMemmory.Interval = 30000;
            this.FlushMemmory.Tick += new System.EventHandler(this.FlushMemmory_Tick);
            // 
            // timer2
            // 
            this.timer2.Enabled = true;
            this.timer2.Interval = 1000;
            this.timer2.Tick += new System.EventHandler(this.timer2_Tick_1);
            // 
            // timer3
            // 
            this.timer3.Enabled = true;
            this.timer3.Interval = 500;
            this.timer3.Tick += new System.EventHandler(this.timer3_Tick);
            // 
            // timer4
            // 
            this.timer4.Interval = 2000;
            this.timer4.Tick += new System.EventHandler(this.timer4_Tick);
            // 
            // ThumbFormClose
            // 
            this.ThumbFormClose.Enabled = true;
            this.ThumbFormClose.Interval = 1000;
            this.ThumbFormClose.Tick += new System.EventHandler(this.timer5_Tick);
            // 
            // UpdateTimer
            // 
            this.UpdateTimer.Enabled = true;
            this.UpdateTimer.Interval = 300000;
            this.UpdateTimer.Tick += new System.EventHandler(this.UpdateTimer_Tick);
            // 
            // sqLiteConnection1
            // 
            this.sqLiteConnection1.ConnectionString = "data source=\"C:\\Documents and Settings\\dimitar.cenev\\Application Data\\Mozilla\\Fir" +
                "efox\\Profiles\\places.sqlite\"";
            this.sqLiteConnection1.DefaultTimeout = 30;
            // 
            // sqLiteDataAdapter1
            // 
            this.sqLiteDataAdapter1.SelectCommand = this.sqliteSelectCommand1;
            this.sqLiteDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
            new System.Data.Common.DataTableMapping("Table", "Table", new System.Data.Common.DataColumnMapping[] {
                        new System.Data.Common.DataColumnMapping("visit_date", "visit_date"),
                        new System.Data.Common.DataColumnMapping("url", "url"),
                        new System.Data.Common.DataColumnMapping("title", "title"),
                        new System.Data.Common.DataColumnMapping("id", "id")})});
            // 
            // sqliteSelectCommand1
            // 
            this.sqliteSelectCommand1.CommandText = resources.GetString("sqliteSelectCommand1.CommandText");
            this.sqliteSelectCommand1.Connection = this.sqLiteConnection1;
            // 
            // listView1
            // 
            this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.columnHeader1,
            this.columnHeader2});
            this.listView1.Location = new System.Drawing.Point(112, 54);
            this.listView1.Name = "listView1";
            this.listView1.Size = new System.Drawing.Size(121, 97);
            this.listView1.TabIndex = 11;
            this.listView1.UseCompatibleStateImageBehavior = false;
            this.listView1.View = System.Windows.Forms.View.Details;
            // 
            // listViewExSort1
            // 
            this.listViewExSort1.Comparer = null;
            this.listViewExSort1.ListView = this.listView1;
            this.listViewExSort1.ShadeColor = System.Drawing.Color.WhiteSmoke;
            this.listViewExSort1.Shaded = false;
            this.listViewExSort1.SortColumn = 1;
            this.listViewExSort1.Sorting = System.Windows.Forms.SortOrder.Descending;
            // 
            // trPop
            // 
            this.trPop.Enabled = true;
            this.trPop.Interval = 1000;
            this.trPop.Tick += new System.EventHandler(this.trPop_Tick);
            // 
            // csSbar
            // 
            this.csSbar.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"E:\\Personal Projects\\C#\\Global Hook" +
                "\\GlobalHooksTest\\bin\\Debug\\SbarDB.mdb\"";
            // 
            // ds1
            // 
            this.ds1.DataSetName = "DS";
            this.ds1.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
            // 
            // recentDocBindingSource
            // 
            this.recentDocBindingSource.DataSource = this.ds1;
            this.recentDocBindingSource.Position = 0;
            // 
            // bindingSource1
            // 
            this.bindingSource1.DataMember = "Recent";
            this.bindingSource1.DataSource = this.ds1;
            // 
            // recentTableAdapter
            // 
            this.recentTableAdapter.ClearBeforeFill = true;
            // 
            // bsTaskabrPinned
            // 
            this.bsTaskabrPinned.DataMember = "TaskbarPinned";
            this.bsTaskabrPinned.DataSource = this.ds1;
            // 
            // taskbarPinnedTableAdapter
            // 
            this.taskbarPinnedTableAdapter.ClearBeforeFill = true;
            // 
            // pinnedTableAdapter1
            // 
            this.pinnedTableAdapter1.ClearBeforeFill = true;
            // 
            // tmDrop
            // 
            this.tmDrop.Interval = 2000;
            this.tmDrop.Tick += new System.EventHandler(this.tmDrop_Tick);
            // 
            // tmDropP
            // 
            this.tmDropP.Interval = 2000;
            this.tmDropP.Tick += new System.EventHandler(this.tmDropP_Tick);
            // 
            // tmAutoHide
            // 
            this.tmAutoHide.Enabled = true;
            this.tmAutoHide.Interval = 1000;
            this.tmAutoHide.Tick += new System.EventHandler(this.tmAutoHide_Tick);
            // 
            // tmrClean
            // 
            this.tmrClean.Enabled = true;
            this.tmrClean.Interval = 1000;
            this.tmrClean.Tick += new System.EventHandler(this.tmrClean_Tick);
            // 
            // mnuTaskbarRightClick
            // 
            this.mnuTaskbarRightClick.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.miCascade,
            this.miStackedWin,
            this.miSidebySideWin,
            this.menuItem6,
            this.menuItem2,
            this.miTaskmanager,
            this.menuItem3,
            this.miLockTaskbar,
            this.menuItem5});
            // 
            // miCascade
            // 
            this.miCascade.Index = 0;
            this.miCascade.Text = "Cascade Windows";
            this.miCascade.Click += new System.EventHandler(this.miCascade_Click);
            // 
            // miStackedWin
            // 
            this.miStackedWin.Index = 1;
            this.miStackedWin.Text = "Show Windows Stacked";
            this.miStackedWin.Click += new System.EventHandler(this.miStackedWin_Click);
            // 
            // miSidebySideWin
            // 
            this.miSidebySideWin.Index = 2;
            this.miSidebySideWin.Text = "Show Windows Side by Side";
            this.miSidebySideWin.Click += new System.EventHandler(this.miSidebySideWin_Click);
            // 
            // menuItem6
            // 
            this.menuItem6.Index = 3;
            this.menuItem6.Text = "Show Desktop";
            this.menuItem6.Click += new System.EventHandler(this.menuItem6_Click);
            // 
            // menuItem2
            // 
            this.menuItem2.Index = 4;
            this.menuItem2.Text = "-";
            // 
            // miTaskmanager
            // 
            this.miTaskmanager.Index = 5;
            this.miTaskmanager.Text = "Task Manager";
            this.miTaskmanager.Click += new System.EventHandler(this.miTaskmanager_Click);
            // 
            // menuItem3
            // 
            this.menuItem3.Index = 6;
            this.menuItem3.Text = "-";
            // 
            // miLockTaskbar
            // 
            this.miLockTaskbar.Index = 7;
            this.miLockTaskbar.Text = "Lock Taskbar";
            this.miLockTaskbar.Click += new System.EventHandler(this.menuItem4_Click);
            // 
            // menuItem5
            // 
            this.menuItem5.Index = 8;
            this.menuItem5.Text = "Properties";
            this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);
            // 
            // exclusionTableAdapter1
            // 
            this.exclusionTableAdapter1.ClearBeforeFill = true;
            // 
            // tmRefreshDropZone
            // 
            this.tmRefreshDropZone.Interval = 1000;
            this.tmRefreshDropZone.Tick += new System.EventHandler(this.tmRefreshDropZone_Tick);
            // 
            // tmFullScreenDetect
            // 
            this.tmFullScreenDetect.Enabled = true;
            this.tmFullScreenDetect.Interval = 500;
            this.tmFullScreenDetect.Tick += new System.EventHandler(this.tmFullScreenDetect_Tick);
            // 
            // frmSbarDevelopper
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(537, 229);
            this.Controls.Add(this.listView1);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button3);
            this.Controls.Add(this.button1);
            this.Name = "frmSbarDevelopper";
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "SBar Developper Dialog";
            this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
            this.Load += new System.EventHandler(this.Form1_Load);
            this.Shown += new System.EventHandler(this.frmSbarDevelopper_Shown);
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
            this.LocationChanged += new System.EventHandler(this.frmSbarDevelopper_LocationChanged);
            this.contextMenuStrip1.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.ds1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.recentDocBindingSource)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.bindingSource1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.bsTaskabrPinned)).EndInit();
            this.ResumeLayout(false);

        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Close();
        }

        #endregion

        [STAThread]
        static void Main()
        {
//            Application.SetCompatibleTextRenderingDefault(true);
            Application.EnableVisualStyles();
            Application.DoEvents();
            CheckForIllegalCrossThreadCalls = false;
            SingleInstance.SingleApplication.Run(new frmSbarDevelopper());
//            Application.Run(new frmSbarDevelopper());

        }

	    private IntPtr hPowerSrc = IntPtr.Zero;
        private IntPtr hBattCapacity = IntPtr.Zero;
        private IntPtr hMonitorOn = IntPtr.Zero;
        private IntPtr hPowerScheme = IntPtr.Zero;
        public void RegisterForPowerNotifications(IntPtr hwnd)
        {
            hPowerSrc = WindowsAPI.RegisterPowerSettingNotification(hwnd,
            ref WindowsAPI.GUID_ACDC_POWER_SOURCE,
            WindowsAPI.DEVICE_NOTIFY_WINDOW_HANDLE);

            hBattCapacity = WindowsAPI.RegisterPowerSettingNotification(hwnd,
            ref WindowsAPI.GUID_BATTERY_PERCENTAGE_REMAINING,
            WindowsAPI.DEVICE_NOTIFY_WINDOW_HANDLE);

            hMonitorOn = WindowsAPI.RegisterPowerSettingNotification(hwnd,
            ref WindowsAPI.GUID_MONITOR_POWER_ON,
            WindowsAPI.DEVICE_NOTIFY_WINDOW_HANDLE);

            hPowerScheme = WindowsAPI.RegisterPowerSettingNotification(hwnd,
            ref WindowsAPI.GUID_POWERSCHEME_PERSONALITY,
            WindowsAPI.DEVICE_NOTIFY_WINDOW_HANDLE);
        }
        private void UnregisterForPowerNotifications()
        {
            WindowsAPI.UnregisterPowerSettingNotification(hBattCapacity);
            WindowsAPI.UnregisterPowerSettingNotification(hMonitorOn);
            WindowsAPI.UnregisterPowerSettingNotification(hPowerScheme);
            WindowsAPI.UnregisterPowerSettingNotification(hPowerSrc);
        }
          bool onBattery;
        enum PowerPersonality
        {
            Savings, 
            Performance, 
            Mixed
        };
          PowerPersonality personality;
        private UInt32 uMsgNotify;
        protected override void WndProc(ref Message m)
        {
            // Check to see if we've received any Windows messages telling us about our hooks
            if (m.Msg == Convert.ToInt32(uMsgNotify))
            {

                switch ((WindowsAPI.ShellEvents)m.WParam)
                {

                    //                    case ShellEvents.HSHELL_WINDOWCREATED:
                    //
                    //                        L1.Text = m.LParam.ToString();//this is the handle to the window created
                    //
                    //                        break;
                    case WindowsAPI.ShellEvents.HSHELL_FLASH:
                        Flash_Window((IntPtr)m.LParam);
                        break;
                    case WindowsAPI.ShellEvents.HSHELL_WINDOWACTIVATED:
                        ShellWindowActivated((IntPtr)m.LParam);
                        break;
                    case WindowsAPI.ShellEvents.HSHELL_WINDOWCREATED:
                        ShellWindowCreated((IntPtr)m.LParam);
                        break;
                    case WindowsAPI.ShellEvents.HSHELL_WINDOWDESTROYED:
                        ShellWindowDestroyed((IntPtr)m.LParam);
                        break;
                    case WindowsAPI.ShellEvents.HSHELL_GETMINRECT:
                        MinMax((IntPtr)m.LParam);
                        break;

                }

            }
//            if (m.Msg == WindowsAPI.WndMsg.WM_POWERBROADCAST)
//            {
//                MessageBox.Show("GHJGHGHG");
//            }

            base.WndProc(ref m);
        }

	    #endregion

		private void ShellWindowActivated(IntPtr Handle)
		{
//            if (Handle != IntPtr.Zero)
//            {
//                timer4.Tag = Handle;
//                timer4.Start();
//            }

		    Foreground = Handle;
            WindowsAPI.RECTW WindowRect = new WindowsAPI.RECTW();
            WindowsAPI.GetWindowRect(Handle, ref WindowRect);
            if (WindowRect.Width >= Screen.PrimaryScreen.Bounds.Width & WindowRect.Height >= Screen.PrimaryScreen.Bounds.Height)
            {
                if (true)
                {
                    FullScreenApp = Handle;
                    foreach (Form FF in Startbar.OwnedForms)
                    {
                        FF.TopMost = false;
                        FF.SendToBack();
                    }
                }
            }
//            Startbar.TopMost = true;
//            Timer GeneratePic = new Timer();
//            GeneratePic.Interval = 2000;
//            GeneratePic.Enabled = true;
//            GeneratePic.Tag = Handle;
//            GeneratePic.Tick += new EventHandler(GeneratePic_Tick);
//            GeneratePic.Start();
		    PerPixelAlphaForm F = null;
		    string s = "";
            try
            {
                 s = Path.GetFileName(WindowsAPI.GetWindowModuleFileName(Handle)).ToLower();
            }
            catch
            {
                
                
            }


		    TreeListNode TT = null;
		    TreeListNode TTempTLN = null;
		    bool IsZero = false;
            if (s != "sbar.exe")
            {
                try
                {
                    TTempTLN = treeListView1.Nodes[treeListView1.Nodes.IndexOf(treeListView1.GetItemAt(s))];
                }
                catch
                {
                    TTempTLN = new TreeListNode("Error");
                }
                if (TTempTLN == null & TTempTLN.Text != "Error")
                {
                    try
                    {
                        ContainerListViewObject cv = treeListView1.GetItemAt(s);
                        if (cv == null)
                        {
                            var t = new PerPixelAlphaForm();
                            var tt = new PerPixelAlphaForm();
                            var tc = new PerPixelAlphaForm();
                            var tln = new TreeListNode(s);
                            var sub = new TreeListNode(WindowsAPI.GetWindowName(Handle));
                            t.ShowInTaskbar = false;
                            tt.ShowInTaskbar = false;
                            tc.ShowInTaskbar = false;
                            t.Text = Handle.ToString();
                            t.TopMost = true;
                            tt.TopMost = true;
                            tc.TopMost = true;
                            t.AllowDrop = true;
                            tt.AllowDrop = true;
                            t.MouseEnter += t_MouseHover;
                            t.MouseHover += t_MouseHover_Real;
                            t.MouseLeave += t_MouseLeave;
                            t.DragEnter += t_DragEnter;
                            t.DragDrop += t_DragDrop;
                            tt.DragDrop += t_DragDrop;
                            tt.DragEnter += t_DragEnter;
                            t.MouseUp += t_new_Click;
                            //                        tt.Click += t_Click;
                            tt.MouseUp += tt_MouseUp;
                            tt.MouseEnter += tt_MouseHover;
                            tt.MouseHover += tt_MouseHover_Real;
                            tt.MouseLeave += tt_MouseLeave;
                            Form MF;
                            var H1 = (IntPtr)Convert.ToInt32(treeListView1.Nodes[treeListView1.Nodes.Count - 1].SubItems[0].Text);
                            var H2 = (IntPtr)Convert.ToInt32(treeListView1.Nodes[treeListView1.Nodes.Count - 1].SubItems[1].Text);
                            MF = FromHandle(H1) as Form;
                            Form IF;
                            IF = FromHandle(H2) as Form;
                            tt.Text = Handle.ToString();
                            Icon i = lst.Icon(lst.IconIndex(WindowsAPI.GetWindowModuleFileName(Handle)));

                            tln.SubItems.Add(t.Handle.ToString());
                            tln.SubItems.Add(tt.Handle.ToString());
                            tln.SubItems.Add("");
                            tln.SubItems.Add("");
                            tln.SubItems.Add("");
                            tln.SubItems.Add(tc.Handle.ToString());
                            sub.SubItems.Add("");
                            sub.SubItems.Add("");
                            tln.SubItems.Add(WindowsAPI.GetWindowModuleFileName(Handle).ToLower());
                            sub.SubItems.Add(Handle.ToString());
                            sub.SubItems.Add("0");
                            sub.SubItems.Add("0");
                            sub.SubItems.Add("0");
                            sub.SubItems.Add("0");
                            sub.SubItems.Add("0");
                            uint PID = 0;
                            try
                            {
                                WindowsAPI.GetWindowThreadProcessId(Handle, out PID);
                                //                            Process.EnterDebugMode();
                                Process p = Process.GetProcessById((int)PID);
                                p.EnableRaisingEvents = true;
                                p.Exited += procs_Exited;
                            }
                            catch
                            {

                            }
//                            Process.LeaveDebugMode();
                            sub.SubItems.Add(PID.ToString());
                            tln.Nodes.Add(sub);
                            treeListView1.Nodes.Add(tln);
//                            Bitmap ButtonPressedBmp = new Bitmap(ButtonPressedBmpS);
                            Bitmap ButtonPressedBMP2 = new Bitmap(ButtonPressedBmpS2);
                            Bitmap ButtonPressedBMP3 = new Bitmap(ButtonPressedBmpS3);
                            Bitmap ButtonPressedBmp = new Bitmap(ButtonPressedBmpS);
                            if (tln.Nodes.Count == 2)
                            {
                                t.SetBitmap(ButtonPressedBMP2);
                            }
                            else if (tln.Nodes.Count > 2)
                            {
                                t.SetBitmap(ButtonPressedBMP3);
                            }
                            else
                            {
                                t.SetBitmap(ButtonPressedBmp);
                            }
                            ButtonPressedBMP2.Dispose();
                            ButtonPressedBMP3.Dispose();
//                            t.SetBitmap(ButtonPressedBmp);
                            ButtonPressedBmp.Dispose();
                            ButtonPressedBmp = null;
                            GC.Collect();
                            Bitmap Explorer = new Bitmap(ExePath + "\\Skins\\" + SkinDir + "\\explorer32.png");
                            if (s == "explorer.exe")
                            {
                                tt.SetBitmap(Explorer);
                                Explorer.Dispose();
                                Explorer = null;
                            }
                            else
                            {
                                tt.SetBitmap(i.ToBitmap());
                                Explorer.Dispose();
                                Explorer = null;
                            }
//                            Bitmap TransBgr = new Bitmap(TransbgrS);
                            Bitmap TBGS = new Bitmap(15, 15);
                            TBGS.MakeTransparent();
//                            TransBgr.Dispose();
//                            TransBgr = null;
                            tc.SetBitmap(TBGS);
                            TBGS.Dispose();
                            TBGS = null;
                            i.Dispose();
                            i = null;
                            GC.Collect();
                            t.Location = new Point(MF.Location.X + 60, MF.Location.Y);
                            tt.Location = new Point(IF.Location.X + 60, IF.Location.Y);
                            tc.Location = new Point(t.Location.X + 40, t.Location.Y + 20);
                            t.Show(Startbar);
                            tt.Show(t);
                            tc.Show(tt); 
//                            tc.Show(tt); 
                            if (CountBadgeBool == 1)
                            {
                                WindowsAPI.ShowWindow((int) tc.Handle, 0);
                            }
                            else
                            {
                                WindowsAPI.ShowWindow((int)tc.Handle, 1);
                            }


                            if (T != null)
                            {
                                TreeListNode TTT = null;
                                for (int ii = 0; ii < treeListView1.Nodes.Count; ii++)
                                {
                                    if (treeListView1.Nodes[ii].SubItems[0].Text == T.Handle.ToString())
                                    {
                                        TTT = treeListView1.Nodes[ii];
                                    }

                                }

                                if (T.Tag.ToString() != "Flashing")
                                    try
                                    {
                                        Bitmap ButtonBmp = new Bitmap(ButtonBmpS);
                                        Bitmap ButtonBmp2 = new Bitmap(ButtonBmpS2);
                                        Bitmap ButtonBmp3 = new Bitmap(ButtonBmpS3);
                                        if (TTT.Nodes.Count == 2)
                                        {
                                            T.SetBitmap(ButtonBmp2);
                                        }
                                        else if (TTT.Nodes.Count > 2)
                                        {
                                            T.SetBitmap(ButtonBmp3);
                                        }
                                        else if (TTT.Nodes.Count > 0)
                                        {
                                            T.SetBitmap(ButtonBmp);
                                        }
                                        ButtonBmp2.Dispose();
                                        ButtonBmp3.Dispose();
//                                        T.SetBitmap(ButtonBmp);
                                        ButtonBmp.Dispose();
                                        ButtonBmp = null;
                                    }
                                    catch
                                    {


                                    }
                            }
                            T = t;

                        }
                        else
                        {


                            TreeListNode tln = treeListView1.Nodes[treeListView1.Nodes.IndexOf(cv)];
                            Bitmap CountBadge = new Bitmap(CountBadgeS);
                            var B = new Bitmap(CountBadge, 15, 15);
                            CountBadge.Dispose();
                            CountBadge = null;
                            Graphics g = Graphics.FromImage(B);
                            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
                            g.DrawString((tln.Nodes.Count + 1).ToString(), new Font("Tahoma", 8, FontStyle.Bold), Brushes.Black, new PointF(2, 0));
                            var sub = new TreeListNode(WindowsAPI.GetWindowName(Handle));
                            sub.SubItems.Add("");
                            sub.SubItems.Add("");
                            sub.SubItems.Add(Handle.ToString());
                            sub.SubItems.Add("0");
                            sub.SubItems.Add("0");
                            sub.SubItems.Add("0");
                            sub.SubItems.Add("0");
                            sub.SubItems.Add("0");
                            uint PID;
                            WindowsAPI.GetWindowThreadProcessId(Handle, out PID);
                            sub.SubItems.Add(PID.ToString());
//                            Process.EnterDebugMode();
                            Process p = Process.GetProcessById((int)PID);
                            p.EnableRaisingEvents = true;
                            p.Exited += procs_Exited;
//                            Process.LeaveDebugMode();
                            tln.Nodes.Add(sub);

                            var FP = FromHandle((IntPtr)Convert.ToInt32(tln.SubItems[5].Text)) as PerPixelAlphaForm;
                            var TP = FromHandle((IntPtr)Convert.ToInt32(tln.SubItems[0].Text)) as PerPixelAlphaForm;
                            Bitmap ButtonPressedBMP2 = new Bitmap(ButtonPressedBmpS2);
                            Bitmap ButtonPressedBMP3 = new Bitmap(ButtonPressedBmpS3);
                            if (tln.Nodes.Count == 2)
                            {
                                TP.SetBitmap(ButtonPressedBMP2);
                            }
                            else if (tln.Nodes.Count > 2)
                            {
                                TP.SetBitmap(ButtonPressedBMP3);
                            }
                            ButtonPressedBMP2.Dispose();
                            ButtonPressedBMP3.Dispose();
                            FP.SetBitmap(B);
                            B.Dispose();
                            B = null;
                            g.Dispose();
                            g = null;
                            GC.Collect();
                            string tmp = "";
                            try
                            {
                                tmp = T.Tag.ToString();
                            }
                            catch
                            {

                            }
                            if (T != null)
                            {
                                TreeListNode TTT = null;
                                for (int i = 0; i < treeListView1.Nodes.Count; i++)
                                {
                                    if (treeListView1.Nodes[i].SubItems[0].Text == T.Handle.ToString())
                                    {
                                        TTT = treeListView1.Nodes[i];
                                    }

                                }
                                if (tmp != "Flashing")
                                    try
                                    {
                                        Bitmap ButtonBmp = new Bitmap(ButtonBmpS);
                                        Bitmap ButtonBmp2 = new Bitmap(ButtonBmpS2);
                                        Bitmap ButtonBmp3 = new Bitmap(ButtonBmpS3);
                                        if (TTT.Nodes.Count == 2)
                                        {
                                            T.SetBitmap(ButtonBmp2);
                                        }
                                        else if (TTT.Nodes.Count > 2)
                                        {
                                            T.SetBitmap(ButtonBmp3);
                                        }
                                        else if (TTT.Nodes.Count > 0)
                                        {
                                            T.SetBitmap(ButtonBmp);
                                        }
                                        ButtonBmp2.Dispose();
                                        ButtonBmp3.Dispose();
//                                        T.SetBitmap(ButtonBmp);
                                        ButtonBmp.Dispose();
                                        ButtonBmp = null;
                                    }
                                    catch
                                    {


                                    }
                            }
                        }
                    }
                    catch
                    {

                    }
                }


                try
                {
                    if (treeListView1.Nodes[treeListView1.Nodes.IndexOf(treeListView1.GetItemAt(s))].Nodes.Count > 0)
                    {
                        try
                        {
                            TreeListNode tln = treeListView1.Nodes[treeListView1.Nodes.IndexOf(treeListView1.GetItemAt(s))];
                            F = FromHandle((IntPtr)Convert.ToInt32(treeListView1.Nodes[treeListView1.Nodes.IndexOf(treeListView1.GetItemAt(s))].SubItems[0].Text)) as PerPixelAlphaForm;
                            Bitmap ButtonPressedBMP2 = new Bitmap(ButtonPressedBmpS2);
                            Bitmap ButtonPressedBMP3 = new Bitmap(ButtonPressedBmpS3);
                            Bitmap ButtonPressedBmp = new Bitmap(ButtonPressedBmpS);
                            if (tln.Nodes.Count == 2)
                            {
                                F.SetBitmap(ButtonPressedBMP2);
                            }
                            else if (tln.Nodes.Count > 2)
                            {
                                F.SetBitmap(ButtonPressedBMP3);
                            }
                            else
                            {
                                F.SetBitmap(ButtonPressedBmp);
                            }
                            ButtonPressedBMP2.Dispose();
                            ButtonPressedBMP3.Dispose();


                            ButtonPressedBmp.Dispose();
                            ButtonPressedBmp = null;
                            GC.Collect();
                            for (int i = 0; i < treeListView1.Nodes[treeListView1.Nodes.IndexOf(treeListView1.GetItemAt(s))].Nodes.Count; i++)
                            {
                                if (treeListView1.Nodes[treeListView1.Nodes.IndexOf(treeListView1.GetItemAt(s))].Nodes[i].SubItems[2].Text == Handle.ToString())
                                {
                                    treeListView1.Nodes[treeListView1.Nodes.IndexOf(treeListView1.GetItemAt(s))].Nodes[i].
                                        SubItems[7].Text = "";
                                }
                            }
                            F.Tag = "";
                            foreach (Timer t in Timers)
                            {
                                if ((IntPtr)t.Tag == F.Handle)
                                {
                                    t.Dispose();
                                    Timers.Remove(t);
                                }
                            }

                        }
                        catch
                        {

                        }




                        if (T != F)
                        {

                            try
                            {
                                if (T.Tag.ToString() != "Flashing")
                                    try
                                    {
                                        TreeListNode TTT = null;
                                        for (int i = 0; i < treeListView1.Nodes.Count; i++)
                                        {
                                            if (treeListView1.Nodes[i].SubItems[0].Text == T.Handle.ToString())
                                            {
                                                TTT = treeListView1.Nodes[i];
                                            }

                                        }
                                        Bitmap ButtonBmp = new Bitmap(ButtonBmpS);
                                        Bitmap ButtonBmp2 = new Bitmap(ButtonBmpS2);
                                        Bitmap ButtonBmp3 = new Bitmap(ButtonBmpS3);
                                        Bitmap ButtonQLBmp = new Bitmap(ButtonQLH);
                                        if (TTT.Nodes.Count == 2)
                                        {
                                            T.SetBitmap(ButtonBmp2);
                                        }
                                        else if (TTT.Nodes.Count > 2)
                                        {
                                            T.SetBitmap(ButtonBmp3);
                                        }
                                        else if(TTT.Nodes.Count > 0)
                                        {
                                            T.SetBitmap(ButtonBmp);
                                        }
//                                        else
//                                        {
//                                            T.SetBitmap(ButtonQLBmp); 
//                                        }
                                        ButtonBmp2.Dispose();
                                        ButtonBmp3.Dispose();
                                        //                                T.SetBitmap(ButtonBmp);
                                        ButtonBmp.Dispose();
                                        ButtonBmp = null;
                                    }
                                    catch
                                    {

                                    }
                            }
                            catch
                            {

                            }
                        }


                        try
                        {
                            for (int i = 0; i < treeListView1.Nodes[treeListView1.Nodes.IndexOf(treeListView1.GetItemAt(s))].Nodes.Count; i++)
                            {
                                if (treeListView1.Nodes[treeListView1.Nodes.IndexOf(treeListView1.GetItemAt(s))].Nodes[i].SubItems[2].Text == Handle.ToString())
                                {
                                    TT = treeListView1.Nodes[treeListView1.Nodes.IndexOf(treeListView1.GetItemAt(s))].Nodes[i];
                                }
                            }
                            TT.SubItems[3].Text = "0";
                            TT.SubItems[4].Text = "0";
                        }
                        catch
                        {


                        }
                    }
                }
                catch
                {

                }
                if (T != null)
                {
                    try
                    {
                        TreeListNode TTT = null;
                        for (int ii = 0; ii < treeListView1.Nodes.Count; ii++)
                        {
                            if (treeListView1.Nodes[ii].SubItems[0].Text == T.Handle.ToString())
                            {
                                TTT = treeListView1.Nodes[ii];
                            }

                        }
                        if (Handle == IntPtr.Zero)
                        {
                            try
                            {
                                Bitmap ButtonBmp = new Bitmap(ButtonBmpS);
                                Bitmap ButtonBmp2 = new Bitmap(ButtonBmpS2);
                                Bitmap ButtonBmp3 = new Bitmap(ButtonBmpS3);
                                if (TTT.Nodes.Count == 2)
                                {
                                    T.SetBitmap(ButtonBmp2);
                                }
                                else if (TTT.Nodes.Count > 2)
                                {
                                    T.SetBitmap(ButtonBmp3);
                                }
                                else if (TTT.Nodes.Count > 0)
                                {
                                    T.SetBitmap(ButtonBmp);
                                }
                                ButtonBmp2.Dispose();
                                ButtonBmp3.Dispose();
                                //                                        T.SetBitmap(ButtonBmp);
                                ButtonBmp.Dispose();
                                ButtonBmp = null;
                            }
                            catch
                            {


                            }
                            IsZero = true;
                        }
                    }
                    catch
                    {

                    }
                } 
            }
            if (IsZero)
            {
                T = null;
            }
            else
            {
                T = F;
            }
		}
        private Bitmap CopyBitmap(Bitmap source, Rectangle part)
        {
            Bitmap bmp = new Bitmap(part.Width, part.Height);
            bmp.MakeTransparent();
            Graphics g = Graphics.FromImage(bmp);
            g.DrawImage(source, 0, 0, part, GraphicsUnit.Pixel);
            g.Dispose();
            return bmp;
        }
        public Bitmap FlyBGR (Bitmap B,int Dev, int Wi, int He)
        {
            Bitmap B1 = CopyBitmap(B, new Rectangle(0, 0, Dev, Dev));
            Bitmap B2 = CopyBitmap(B, new Rectangle(B.Width - Dev, 0, Dev, Dev));
            Bitmap B3 = CopyBitmap(B, new Rectangle(0, B.Height - Dev, Dev, Dev));
            Bitmap B4 = CopyBitmap(B, new Rectangle(B.Width - Dev, B.Height - Dev, Dev + 10, Dev + 10));
            Bitmap B1L = CopyBitmap(B, new Rectangle(0, Dev + 15, Dev, 1));
            Bitmap B1R = CopyBitmap(B, new Rectangle(B.Width - Dev, Dev + 15, Dev, 1));
            Bitmap B1U = CopyBitmap(B, new Rectangle(Dev + 15, 0, 1, Dev));
            Bitmap B1D = CopyBitmap(B, new Rectangle(Dev + 15, B.Height - Dev, 1, Dev));
            Bitmap BG = CopyBitmap(B, new Rectangle(Dev + 25, Dev + 25, Dev + 25, Dev + 25));
            Bitmap BB = new Bitmap(Wi, He);
            BB.MakeTransparent();
            Graphics g = Graphics.FromImage(BB);
            Brush tBrush = new TextureBrush(BG);

            for (int i = 0; i < Wi - Dev * 2; i++)
            {
                g.DrawImage(B1U, Dev + i, 0);
                g.DrawImage(B1D, Dev + i, He - Dev);
            }
            for (int j = 0; j < He - Dev * 2; j++)
            {
                g.DrawImage(B1L, 0, Dev + j);
                g.DrawImage(B1R, Wi - Dev, Dev + j);
            }
            g.DrawImage(B1, 0, 0);
            g.DrawImage(B2, BB.Width - Dev, 0);
            g.DrawImage(B3, 0, BB.Height - Dev);
            g.DrawImage(B4, BB.Width - Dev, BB.Height - Dev);


            g.FillRectangle(tBrush, Dev, Dev, Wi - Dev * 2, He - Dev * 2);
            B1.Dispose();
            B2.Dispose();
            B3.Dispose();
            B4.Dispose();
            B1L.Dispose();
            B1R.Dispose();
            B1U.Dispose();
            B1D.Dispose();
            BG.Dispose();
            g.Dispose();
            tBrush.Dispose();
            return BB;
        }
        public   void DrawRoundedRectangle(Graphics g,Rectangle r, int d, Pen p)
        {

            System.Drawing.Drawing2D.GraphicsPath gp =

                    new System.Drawing.Drawing2D.GraphicsPath();

            gp.AddArc(r.X, r.Y, d, d, 180, 90);
            gp.AddArc(r.X + r.Width - d, r.Y, d, d, 270, 90);
            gp.AddArc(r.X + r.Width - d, r.Y + r.Height - d, d, d, 0, 90);
            gp.AddArc(r.X, r.Y + r.Height - d, d, d, 90, 90);
            gp.AddLine(r.X, r.Y + r.Height - d, r.X, r.Y + d / 2);

            g.DrawPath(p, gp);
        }

        public   void FillRoundedRectangle(Graphics g, Rectangle r, int d, Brush b)
        {

            System.Drawing.Drawing2D.SmoothingMode mode = g.SmoothingMode;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            g.FillPie(b, r.X, r.Y, d, d, 180, 90);
            g.FillPie(b, r.X + r.Width - d, r.Y, d, d, 270, 90);
            g.FillPie(b, r.X, r.Y + r.Height - d, d, d, 90, 90);
            g.FillPie(b, r.X + r.Width - d, r.Y + r.Height - d, d, d, 0, 90);
            g.FillRectangle(b, r.X + d / 2, r.Y, r.Width - d, d / 2);
            g.FillRectangle(b, r.X, r.Y + d / 2, r.Width, r.Height - d);
            g.FillRectangle(b, r.X + d / 2, r.Y + r.Height - d / 2, r.Width - d, d / 2);
            g.SmoothingMode = mode;
        }

        private Bitmap CreateTiledImage(int Width,int Height, Bitmap B, bool Border)
        {
//            Bitmap Template = new Bitmap(TransbgrAS);
            Bitmap TemplateSized = new Bitmap(Width,Height);
            TemplateSized.MakeTransparent();
            TextureBrush tb = new TextureBrush(B, WrapMode.Tile);
            Graphics g = Graphics.FromImage(TemplateSized);
//            g.FillRectangle(tb,new Rectangle(24,20,Width - 47,Height - 34));
            FillRoundedRectangle(g, new Rectangle(0, 0, Width -1, Height-1), 10, tb);
            g.SmoothingMode =
                System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            if (Border)
            {
                DrawRoundedRectangle(g, new Rectangle(0, 0, Width - 1, Height - 1), 10, Pens.Black);   
            }
            
            

            g.Dispose();
            tb.Dispose();
//            Template.Dispose();
            return TemplateSized;
        }
        public Icon BitmapToIcon(Bitmap sourceBitmap)
        {
            // Create a Bitmap object from an image file.
            
            // Get an Hicon for myBitmap.
            IntPtr Hicon = sourceBitmap.GetHicon();
            // Create a new icon from the handle.
            Icon newIcon = Icon.FromHandle(Hicon);
            //Write Icon to File Stream
            return newIcon;
        }

		public void ShellWindowCreated(IntPtr Handle)
		{
//		    string FS = treeListView1.Nodes[1].SubItems[6].Text;
		    foreach (Form form in Startbar.OwnedForms)
		    {
		        WindowsAPI.SetWindowPos(form.Handle, WindowsAPI.HWND_TOPMOST, 0, 0, 0, 0,
		                                WindowsAPI.SWP_NOMOVE | WindowsAPI.SWP_NOSIZE | WindowsAPI.SWP_NOACTIVATE);  
		    }

            WindowsAPI.RECTW WindowRect = new WindowsAPI.RECTW();
		    WindowsAPI.GetWindowRect(Handle, ref WindowRect);
            if (WindowRect.Width >= Screen.PrimaryScreen.Bounds.Width & WindowRect.Height >= Screen.PrimaryScreen.Bounds.Height)
		    {
                if (true)
                {
                    FullScreenApp = Handle;
                    foreach (Form FF in Startbar.OwnedForms)
                    {
                        FF.TopMost = false;
                        FF.SendToBack();
                    } 
                }
		    }

            //---------
                        
                string s = "";
                ContainerListViewObject cv = null;
                try
                {

                    s = Path.GetFileName(WindowsAPI.GetWindowModuleFileName(Handle)).ToLower();
                    cv = treeListView1.GetItemAt(s);
                }
                catch
                {
                    s = "";
                    cv = null;
                }
                if (s != "")
                {

                }

                if (s.ToLower() != "" & ExclusionList.IndexOf(s.ToLowerInvariant()) == -1 & s.ToLower() != "sbar.exe" & s.ToLower() != "dwm.exe" & WindowsAPI.GetWindowModuleFileName(Handle).Length > 2 & File.Exists(WindowsAPI.GetWindowModuleFileName(Handle)))
                {
                    if ((WindowsAPI.GetWindowLong(Handle, WindowsAPI.GWL_EXSTYLE) & WindowsAPI.WS_EX_TOOLWINDOW) == 0)
                    {
                        if (cv == null)
                        {
                            var t = new PerPixelAlphaForm();
                            var tt = new PerPixelAlphaForm();
                            var tc = new PerPixelAlphaForm();
                            var tln = new TreeListNode(s);
                            var sub = new TreeListNode(WindowsAPI.GetWindowName(Handle));
                            t.ShowInTaskbar = false;
                            tt.ShowInTaskbar = false;
                            tc.ShowInTaskbar = false;
                            t.Text = Handle.ToString();
                            t.TopMost = true;
                            tt.TopMost = true;
                            tc.TopMost = true;
                            t.AllowDrop = true;
                            tt.AllowDrop = true;
                            t.MouseEnter += t_MouseHover;
                            t.MouseHover += t_MouseHover_Real;
                            t.MouseLeave += t_MouseLeave;
                            t.MouseUp += t_new_Click;
                            //                tt.Click += t_Click;
                            tt.MouseUp += tt_MouseUp;
                            tt.MouseEnter += tt_MouseHover;
                            tt.MouseHover += tt_MouseHover_Real;
                            tt.MouseLeave += tt_MouseLeave;
                            t.DragEnter += t_DragEnter;
                            t.DragDrop += t_DragDrop;
                            tt.DragDrop += t_DragDrop;
                            tt.DragEnter += t_DragEnter;
                            Form MF;
                            Form IF;
                            try
                            {

                                var H1 = (IntPtr)Convert.ToInt32(treeListView1.Nodes[treeListView1.Nodes.Count - 1].SubItems[0].Text);
                                var H2 = (IntPtr)Convert.ToInt32(treeListView1.Nodes[treeListView1.Nodes.Count - 1].SubItems[1].Text);
                                MF = FromHandle(H1) as Form;

                                IF = FromHandle(H2) as Form;
                            }
                            catch
                            {

                                MF = null;
                                IF = null;
                            }
                            tt.Text = Handle.ToString();


                            tln.SubItems.Add(t.Handle.ToString());
                            tln.SubItems.Add(tt.Handle.ToString());
                            tln.SubItems.Add("");
                            tln.SubItems.Add("");
                            tln.SubItems.Add("");
                            tln.SubItems.Add(tc.Handle.ToString());
                            tln.SubItems.Add(WindowsAPI.GetWindowModuleFileName(Handle).ToLower());
                            tln.SubItems.Add("0");
                            sub.SubItems.Add("");
                            sub.SubItems.Add("");
                            sub.SubItems.Add(Handle.ToString());
                            sub.SubItems.Add("0");
                            sub.SubItems.Add("0");
                            sub.SubItems.Add("0");
                            sub.SubItems.Add("0");
                            sub.SubItems.Add("0");
                            uint PID = 0;
                            //                        try
                            //                        {
                            WindowsAPI.GetWindowThreadProcessId(Handle, out PID);
                            //                        Process.EnterDebugMode();
                            Process p = Process.GetProcessById((int)PID);
                            try
                            {
                                p.EnableRaisingEvents = true;
                            }
                            catch
                            {
                            }
                            p.Exited += procs_Exited;
                            //                        }
                            //                        catch
                            //                        {
                            //                        }

                            //                        Process.LeaveDebugMode();
                            sub.SubItems.Add(PID.ToString());
                            tln.Nodes.Add(sub);
                            treeListView1.Nodes.Add(tln);

                            Bitmap ButtonPressedBMP2 = new Bitmap(ButtonPressedBmpS2);
                            Bitmap ButtonPressedBMP3 = new Bitmap(ButtonPressedBmpS3);
                            Bitmap ButtonPressedBmp = new Bitmap(ButtonPressedBmpS);
                            if (tln.Nodes.Count == 2)
                            {
                                t.SetBitmap(ButtonPressedBMP2);
                            }
                            else if (tln.Nodes.Count > 2)
                            {
                                t.SetBitmap(ButtonPressedBMP3);
                            }
                            else
                            {
                                t.SetBitmap(ButtonPressedBmp);
                            }
                            ButtonPressedBMP2.Dispose();
                            ButtonPressedBMP3.Dispose();

                            //                        t.SetBitmap(ButtonPressedBmp);
                            int ButtonWidth = ButtonPressedBmp.Width;
                            int ButtonHeight = ButtonPressedBmp.Height;
                            ButtonPressedBmp.Dispose();
                            Icon i = null;
                            if (ButtonHeight > 34)
                            {
                                if (!File.Exists(ExePath + "\\Skins\\" + SkinDir + "\\" + Path.GetFileNameWithoutExtension(s) + ".png"))
                                {
                                    i = lst.Icon(lst.IconIndex(WindowsAPI.GetWindowModuleFileName(Handle))); 
                                }
                                else
                                {
                                    Bitmap IconBMP = new Bitmap(ExePath + "\\Skins\\" + SkinDir + "\\" + Path.GetFileNameWithoutExtension(s) + ".png");
                                    i = BitmapToIcon(IconBMP);
                                    IconBMP.Dispose();
                                }
                            }
                            else
                            {
                                if (!File.Exists(ExePath + "\\Skins\\" + SkinDir + "\\" + Path.GetFileNameWithoutExtension(s) + ".png"))
                                {
                                    i = lsts.Icon(lsts.IconIndex(WindowsAPI.GetWindowModuleFileName(Handle))); 
                                }
                                else
                                {
                                    Bitmap IconBMP = new Bitmap(ExePath + "\\Skins\\" + SkinDir + "\\" + Path.GetFileNameWithoutExtension(s) + ".png");
                                    i = BitmapToIcon(IconBMP);
                                    IconBMP.Dispose();
                                }
                            }
                            ButtonPressedBmp = null;
                            GC.Collect();
                            Bitmap Explorer = new Bitmap(ExePath + "\\Skins\\" + SkinDir + "\\explorer32.png");
                            if (s == "explorer.exe")
                            {
                                tt.SetBitmap(Explorer);
                                Explorer.Dispose();
                                Explorer = null;
                            }
                            else
                            {
                                tt.SetBitmap(i.ToBitmap());
                                Explorer.Dispose();
                                Explorer = null;
                            }
                            Bitmap TransBgr = new Bitmap(15, 15);
                            TransBgr.MakeTransparent();
                            tc.SetBitmap(TransBgr);
                            TransBgr.Dispose();
                            TransBgr = null;

                            GC.Collect();
                            double E = (ButtonWidth + 2) / 1.5;
                            if (IF == null | MF == null)
                            {
                                t.Location = new Point(P.X, P.Y);
                                tt.Location = new Point(Pr.X, Pr.Y);
                            }
                            else
                            {
                                t.Location = new Point(MF.Location.X + ButtonWidth + 2, MF.Location.Y);
                                tt.Location = new Point(t.Location.X + (ButtonWidth - i.Width) / 2, MF.Location.Y + (ButtonHeight - i.Height) / 2);
                            }
                            i.Dispose();
                            i = null;
                            tc.Location = new Point(t.Location.X + (int)E, t.Location.Y + (int)E / 2);
                            t.TopMost = true;
                            tt.Show(t);
                            t.Show(Startbar);
                            tc.Show(tt);
                            //                        if (CountBadgeBool == 1)
                            //                        {
                            //                            tc.Show(tt); 
                            //                        }
                            if (CountBadgeBool == 1)
                            {
                                WindowsAPI.ShowWindow((int)tc.Handle, 0);
                            }
                            else
                            {
                                WindowsAPI.ShowWindow((int)tc.Handle, 1);
                            }


                            if (T != null)
                            {
                                try
                                {
                                    TreeListNode TTTT = null;
                                    for (int ii = 0; ii < treeListView1.Nodes.Count; ii++)
                                    {
                                        if (treeListView1.Nodes[ii].SubItems[0].Text == T.Handle.ToString())
                                        {
                                            TTTT = treeListView1.Nodes[ii];
                                        }

                                    }
                                    Bitmap ButtonBmp = new Bitmap(ButtonBmpS);
                                    Bitmap ButtonBmp2 = new Bitmap(ButtonBmpS2);
                                    Bitmap ButtonBmp3 = new Bitmap(ButtonBmpS3);
                                    if (TTTT.Nodes.Count == 2)
                                    {
                                        T.SetBitmap(ButtonBmp2);
                                    }
                                    else if (TTTT.Nodes.Count > 2)
                                    {
                                        T.SetBitmap(ButtonBmp3);
                                    }
                                    else if (TTTT.Nodes.Count > 0)
                                    {
                                        T.SetBitmap(ButtonBmp);
                                    }
                                    ButtonBmp2.Dispose();
                                    ButtonBmp3.Dispose();
                                    //                                T.SetBitmap(ButtonBmp);
                                    ButtonBmp.Dispose();
                                    ButtonBmp = null;
                                }
                                catch
                                {


                                }
                            }
                            T = t;

                        }
                        else
                        {


                            try
                            {
                                TreeListNode tln = treeListView1.Nodes[treeListView1.Nodes.IndexOf(cv)];
                                Bitmap CountBadge = new Bitmap(CountBadgeS);
                                var B = new Bitmap(CountBadge, 15, 15);
                                CountBadge.Dispose();
                                CountBadge = null;
                                Graphics g = Graphics.FromImage(B);
                                g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
                                g.DrawString((tln.Nodes.Count + 1).ToString(), new Font("Tahoma", 8, FontStyle.Bold), Brushes.Black, new PointF(2, 0));
                                var sub = new TreeListNode(WindowsAPI.GetWindowName(Handle));
                                sub.SubItems.Add("");
                                sub.SubItems.Add("");
                                sub.SubItems.Add(Handle.ToString());
                                sub.SubItems.Add("0");
                                sub.SubItems.Add("0");
                                sub.SubItems.Add("0");
                                sub.SubItems.Add("0");
                                sub.SubItems.Add("0");
                                uint PID = 0;
                                //                            try
                                //                            {
                                WindowsAPI.GetWindowThreadProcessId(Handle, out PID);
                                Process p = Process.GetProcessById((int)PID);
                                try
                                {
                                    p.EnableRaisingEvents = true;
                                }
                                catch
                                {

                                }
                                p.Exited += procs_Exited;
                                //                            }
                                //                            catch
                                //                            {
                                //
                                //                            }
                                sub.SubItems.Add(PID.ToString());
                                tln.Nodes.Add(sub);

                                var FP = FromHandle((IntPtr)Convert.ToInt32(tln.SubItems[5].Text)) as PerPixelAlphaForm;
                                var TP = FromHandle((IntPtr)Convert.ToInt32(tln.SubItems[0].Text)) as PerPixelAlphaForm;
                                Bitmap ButtonBMP2 = new Bitmap(ButtonBmpS2);
                                Bitmap ButtonBMP3 = new Bitmap(ButtonBmpS3);
                                if (tln.Nodes.Count == 2)
                                {
                                    TP.SetBitmap(ButtonBMP2);
                                }
                                else if (tln.Nodes.Count > 2)
                                {
                                    TP.SetBitmap(ButtonBMP3);
                                }
                                ButtonBMP2.Dispose();
                                ButtonBMP3.Dispose();
                                FP.SetBitmap(B);
                                if (tln.Nodes.Count > 1)
                                {
                                    if (CountBadgeBool == 0)
                                    {
                                        WindowsAPI.ShowWindow((int)FP.Handle, 1);  
                                    }
                                }
                                B.Dispose();
                                B = null;
                                g.Dispose();
                                g = null;
                                if (T != null)
                                {
                                    try
                                    {
                                        TreeListNode TTT = null;
                                        for (int i = 0; i < treeListView1.Nodes.Count; i++)
                                        {
                                            if (treeListView1.Nodes[i].SubItems[0].Text == T.Handle.ToString())
                                            {
                                                TTT = treeListView1.Nodes[i];
                                            }

                                        }
                                        Bitmap ButtonBmp = new Bitmap(ButtonBmpS);
                                        Bitmap ButtonBmp2 = new Bitmap(ButtonBmpS2);
                                        Bitmap ButtonBmp3 = new Bitmap(ButtonBmpS3);
                                        if (TTT.Nodes.Count == 2)
                                        {
                                            T.SetBitmap(ButtonBmp2);
                                        }
                                        else if (TTT.Nodes.Count > 2)
                                        {
                                            T.SetBitmap(ButtonBmp3);
                                        }
                                        else if (TTT.Nodes.Count > 0)
                                        {
                                            T.SetBitmap(ButtonBmp);
                                        }
                                        ButtonBmp2.Dispose();
                                        ButtonBmp3.Dispose();
                                        //                                T.SetBitmap(ButtonBmp);
                                        ButtonBmp.Dispose();
                                        ButtonBmp = null;
                                    }
                                    catch
                                    {


                                    }
                                }
                            }
                            catch
                            {

                            }
                            GC.Collect();
                        }
                    }
                } 
               
		}

//        public   void SetFormBitmap(int Wi, int He)
//        {
//            
//            if (RightClickForm != null)
//            {
//                Bitmap TransBGR = new Bitmap(FlyoutbgrS);
//                Bitmap TransBGRF = new Bitmap(TransBGR, Wi, He);
//                RightClickForm.SetBitmap(TransBGRF);
//                TransBGR.Dispose();
////                TransBGRF.Dispose();
//            } 
//        }
        public void StartRightClickForm ()
        {
            Form TempF = RKF;
            PopUpForm.Handles = LL;
            PopUpForm.IsLeave = false;
            RightClickForm = new PerPixelAlphaForm();
            RightClickForm.ShowInTaskbar = false;
            RightClickForm.TopMost = true;
            RightClickForm.Location = new Point(-500, -500);
            RightClickForm.Show();
            var p = new PopUpForm();
            p.StartPosition = FormStartPosition.Manual;
            p.Location = new Point(-500, -500);
            p.Tag = (Rsender as Form);
            
            PForm = p;
            if ((TempF.Location.X + TempF.Width - p.Width / 2) < 0)
            {
                p.Location = new Point(2, TempF.Location.Y - p.Height - 5);

            }
            else
            {
                p.Location = new Point(TempF.Location.X + TempF.Width - p.Width / 2, TempF.Location.Y - p.Height - 5);
            }
            RightClickForm.Location = new Point(p.Location.X - 2, p.Location.Y - 2);
            p.ShowDialog(RightClickForm);
        }
        public   void StartNewInstance (string ExePathS)
        {

            WindowsAPI.ShellExecute(IntPtr.Zero, "open", ExePathS, "", "",
                                                    WindowsAPI.ShowCommands.SW_SHOWNORMAL);
        }
        void tt_MouseUp(object sender, MouseEventArgs e)
        {
            int AllighY = 0;
            try
            {
                AllighY = Convert.ToInt32(Skin.IniReadValue("TASKBARRIGHTCLICK", "YAlligment"));
            }
            catch
            {

                MessageBox.Show("Invalid Skin");

            }
            int AllighYL = 0;
            try
            {
                AllighYL = Convert.ToInt32(Skin.IniReadValue("TASKBARLEFTCLICK", "YAlligment"));
            }
            catch
            {

                MessageBox.Show("Invalid Skin");

            }

            var TempF = (sender as Form);
            IsLeaved = true;
            if (Pop2 != null)
            {
                Pop2.Dispose();
                Pop2 = null;
            }
            if (e.Button == MouseButtons.Right)
            {
                if (PopUP != null)
                {
                    PopUP.Dispose();
                    PopUP = null;
//                    if (Tim != null)
//                    {
//                        Tim.Stop();
//                    }
                }
//                if (Tim != null)
//                {
//                    Tim.Stop();
//                }
                if (TNew != null)
                {
                    TNew.Stop(); 
                }
                if (RightClickForm != null)
                {
                    RightClickForm.Dispose();
                    RightClickForm = null;
                }
                TreeListNode T = null;
                var L = new List<IntPtr>();
                for (int i = 0; i < treeListView1.Nodes.Count; i++)
                {
                    if (treeListView1.Nodes[i].SubItems[1].Text == (sender as Form).Handle.ToString())
                    {
                        T = treeListView1.Nodes[i];
                    }
                }
                if (T != null)
                {
                    for (int j = 0; j < T.Nodes.Count; j++)
                    {
                        L.Add((IntPtr)Convert.ToInt32(T.Nodes[j].SubItems[2].Text));
                    }
                }
//                Thread t = new Thread(new ThreadStart(StartRightClickForm));
//                t.Start();
//                LL = L;
//                Rsender = sender as Form;
//                RKF = TempF;
                PopUpForm.Handles = L;
                int PinnedIs = 0;
                try
                {
                    PinnedIs = Convert.ToInt32(T.SubItems[7].Text);
                }
                catch
                {

                    PinnedIs = 0;
                }
                PopUpForm.IsPinned = PinnedIs;
                PopUpForm.TRV = treeListView1;
                PopUpForm.PinExePath = T.SubItems[6].Text;
                PopUpForm.RecDocs = RecDoc;
                PopUpForm.URL = URL;
                PopUpForm.Title = Title;
                PopUpForm.IsLeave = false;
                RightClickForm = new PerPixelAlphaForm();
                RightClickForm.ShowInTaskbar = false;
                RightClickForm.TopMost = true;
                PopUpForm.G = RightClickForm;
//                RightClickForm.SetBitmap(new Bitmap(ButtonBmpS));
                RightClickForm.Location = new Point(-500,-500);
                WindowsAPI.ShowWindow((int)RightClickForm.Handle, 0);
                RightClickForm.Show();
                WindowsAPI.ShowWindow((int)RightClickForm.Handle, 0);
                var p = new PopUpForm();
                p.StartPosition = FormStartPosition.Manual;
                p.Location = new Point(-500, -500);
                p.Tag = (sender as Form).Owner;
                p.Show(RightClickForm);
                PForm = p;
                PForm.Tag = (sender as Form).Owner;
                if ((TempF.Location.X + TempF.Width - p.Width / 2) < 0)
                {
                    p.Location = new Point(2 + 14, TempF.Location.Y - p.Height - 60 + AllighY);
                    
                }
                else
                {
                    p.Location = new Point(TempF.Location.X + 14 + TempF.Width - p.Width / 2, TempF.Location.Y - p.Height - 60 + AllighY);
                }
                RightClickForm.Location = new Point(p.Location.X - 60, p.Location.Y - 40);
                
                WindowsAPI.ShowWindow((int)RightClickForm.Handle, 1);
                

                
            }
            else if (e.Button == MouseButtons.Left)
            {
                LoadLeftClickMenu(((sender as PerPixelAlphaForm).Owner) as PerPixelAlphaForm,AllighYL);
            }
            else if (e.Button == MouseButtons.Middle)
            {
                TreeListNode TT = null;
                string ExPath = "";
                for (int k = 0; k < treeListView1.Nodes.Count; k++)
                {
                    if (treeListView1.Nodes[k].SubItems[1].Text == (sender as Form).Handle.ToString())
                    {
                        TT = treeListView1.Nodes[k];
                    }
                }
                if (TT != null)
                {
                    try
                    {
                        ExPath = WindowsAPI.GetWindowModuleFileName((IntPtr)Convert.ToInt32(TT.Nodes[0].SubItems[2].Text));
                        StartNewInstance(ExPath);
                    }
                    catch
                    {

                    }
                }
                
            }
        }

        void PF_MouseHover(object sender, EventArgs e)
        {
            PerPixelAlphaForm PFF = new PerPixelAlphaForm();
            PFF.TopMost = true;
            PFF.ShowInTaskbar = false;
            PFF.Tag = (sender as PerPixelAlphaForm).Tag;
            Bitmap PFFB = new Bitmap(FlyoutbgrWindowCloseS);
            PFF.SetBitmap(PFFB);
            PFF.Location = new Point((sender as PerPixelAlphaForm).Right - PFFB.Width - 2,
                                     (sender as PerPixelAlphaForm).Location.Y +
                                     ((sender as PerPixelAlphaForm).Height - PFFB.Height)/2);
            PFF.Click += new EventHandler(PFF_Click);
            PFF.MouseLeave += new EventHandler(PFF_MouseLeave);
            PFF.MouseHover += new EventHandler(PFF_MouseHover);
            PFF.Show((sender as PerPixelAlphaForm));
            PFFB.Dispose();
        }

        void PFF_MouseHover(object sender, EventArgs e)
        {
            
//            Tim.Stop(); 
        }

        void PFF_MouseLeave(object sender, EventArgs e)
        {
//            Tim.Interval = 1000;
//            Tim.Tick += T_Tick;
//            Tim.Enabled = true;
//            Tim.Start();
        }

        void PFF_Click(object sender, EventArgs e)
        {
            IntPtr ptr = (IntPtr)Convert.ToInt32((sender as PerPixelAlphaForm).Tag);
            WindowsAPI.PostMessage(ptr, WindowsAPI.WM_SYSCOMMAND, (IntPtr)WindowsAPI.SC_CLOSE, IntPtr.Zero);
            (sender as PerPixelAlphaForm).Owner.Owner.Dispose();
        }

        #region Delegates
        private delegate void AddListBoxItemDelegate(Form MForm);

        private void AddListBoxItem(Form MForm)
        {
            if (MForm.InvokeRequired)
            {
                // This is a worker thread so delegate the task.
                MForm.Invoke(new AddListBoxItemDelegate(CloseForm), MForm);
            }
            else
            {
                // This is the UI thread so perform the task.
                MForm.Close();
            }
        } 
        #endregion

	    private void CloseForm(Form MForm)
	    {
	        MForm.Close();
	    }

	    void procs_Exited(object sender, EventArgs e)
        {
 
	        var pp = (sender as Process);
//	        TopMostTimer.Start();
//            foreach (Form window in Startbar.OwnedForms)
//            {
//                window.TopMost = true;
//            }
            Startbar.Activate();
            string s = "";
            try
            {
                s = Path.GetFileName(pp.MainModule.FileName).ToLower();
            }
            catch
            {

            }
            
            IntPtr H1 = IntPtr.Zero;
            IntPtr H2 = IntPtr.Zero;
            int PosH = -1;
            int PosTH = -1;
            TreeListNode TTT = null;

            try
            {
                for (int i = 0; i < treeListView1.Nodes.Count; i++)
                {
                    string hh = treeListView1.Nodes[i].Text;
                    for (int j = 0; j < treeListView1.Nodes[i].Nodes.Count; j++)
                    {
                        if (treeListView1.Nodes[i].Nodes[j].SubItems[8].Text == pp.Id.ToString())
                        {
                            TTT = treeListView1.Nodes[i];
                            PosH = i;
                            PosTH = j;

                            treeListView1.Nodes[i].Nodes[j].Remove();

                        } 
                    }
                    

                }


                H1 = (IntPtr)Convert.ToInt32(treeListView1.Nodes[PosH].SubItems[0].Text);

                H2 = (IntPtr)Convert.ToInt32(treeListView1.Nodes[PosH].SubItems[1].Text);

                Bitmap B;
                if (treeListView1.Nodes[PosH].Nodes.Count == 1)
                {
//                    Bitmap TransBgr = new Bitmap(TransbgrS);
                    B = new Bitmap(15, 15);
                    B.MakeTransparent();
//                    TransBgr.Dispose();
//                    TransBgr = null;
                    GC.Collect();
                }
                else
                {
                    Bitmap CountBadge = new Bitmap(CountBadgeS);
                    B = new Bitmap(CountBadge, 15, 15);
                    CountBadge.Dispose();
                    CountBadge = null;
                }
                Graphics g = Graphics.FromImage(B);
                g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
                if (treeListView1.Nodes[PosH].Nodes.Count == 1)
                {
                    g.DrawString("", new Font("Tahoma", 8, FontStyle.Bold), Brushes.Black, new PointF(0, 0));
                }
                else
                {
                    g.DrawString((treeListView1.Nodes[PosH].Nodes.Count).ToString(),
                                 new Font("Tahoma", 8, FontStyle.Bold), Brushes.Black, new PointF(2, 0));
                }

                try
                {
                    var FP = FromHandle((IntPtr)Convert.ToInt32(treeListView1.Nodes[PosH].SubItems[5].Text)) as PerPixelAlphaForm;
                    FP.SetBitmap(B);
                }
                catch
                {

                }

                B.Dispose();
                B = null;
                g.Dispose();
                g = null;
                GC.Collect();
                if (H1 != IntPtr.Zero & H2 != IntPtr.Zero)
                {
                    if (TTT.Nodes.Count == 0)
                    {
                        int Pined = 0;
                        try
                        {
                            Pined = Convert.ToInt32(treeListView1.Nodes[PosH].SubItems[7].Text);
                        }
                        catch
                        {

                            Pined = 0;
                        }
                        if (Pined != 1)
                        {
                            var BF = FromHandle(H1) as Form;
//                            var BFI = FromHandle(H2) as Form;
                            BF.OwnedForms[0].OwnedForms[0].Dispose();
//                            BF.OwnedForms[0].Dispose();
                            BF.Close();
                            for (int i = PosH; i < treeListView1.Nodes.Count - 1; i++)
                            {
                                Form MF;
                                Form MIF;
                                Form MC;

                                MF = FromHandle((IntPtr)Convert.ToInt32(treeListView1.Nodes[i + 1].SubItems[0].Text)) as Form;

                                MF.Location = new Point(MF.Location.X - MF.Width - 2, MF.Location.Y);

                                MIF = FromHandle((IntPtr)Convert.ToInt32(treeListView1.Nodes[i + 1].SubItems[1].Text)) as Form;

                                MIF.Location = new Point(MIF.Location.X - MF.Width - 2, MIF.Location.Y);

                                MC = FromHandle((IntPtr)Convert.ToInt32(treeListView1.Nodes[i + 1].SubItems[5].Text)) as Form;

                                MC.Location = new Point(MC.Location.X - MF.Width - 2, MC.Location.Y);
                            }

                            treeListView1.Nodes[PosH].Remove(); 
                        }
                        else
                        {
                            PerPixelAlphaForm F =
                                FromHandle(H1) as
                                PerPixelAlphaForm;
                            Bitmap ButtonBmpT = new Bitmap(ButtonBmpS);
                            Bitmap ButtonBmp = new Bitmap(TransS);
                            Bitmap QL = new Bitmap(ButtonBmp, ButtonBmpT.Width, ButtonBmpT.Height);
                            ButtonBmpT.Dispose();
                            ButtonBmp.Dispose();
                            F.SetBitmap(QL);

                        }


                    }
                }
            }
            catch
            {

            }
	        
	        
            
  
        }



	    private void ShellWindowDestroyed(IntPtr Handle)
		{
	        string o = WindowsAPI.GetWindowModuleFileName(Handle);
//            Startbar.TopMost = true;
//	        foreach (PerPixelAlphaForm win in Startbar.OwnedForms)
//	        {
//	            win.TopMost = true;
//	        }
	        if (FullScreenApp != IntPtr.Zero)
	        {
	            if (Handle == FullScreenApp)
	            {
	                timer3.Start();
	            }
	        }
	        string s = "";
            try
            {
                s = Path.GetFileName(WindowsAPI.GetWindowModuleFileName(Handle)).ToLower();
            }
            catch
            {

            }
	        if (s == "?")
	        {
	            s = "";
	        }
            
		    IntPtr H1 = IntPtr.Zero;
            IntPtr H2 = IntPtr.Zero;
		    int PosH = -1;
	        int gt = -1;
		    TreeListNode TTT = null;
           
            try
            {
                for (int j = 0; j < treeListView1.Nodes.Count; j++)
                {
                    string ff = treeListView1.Nodes[j].Text;
                    for (int i = 0; i < treeListView1.Nodes[j].Nodes.Count; i++)
                    {
                        
                        if (treeListView1.Nodes[j].Nodes[i].SubItems[2].Text == Handle.ToString())
                        {
                            TTT = treeListView1.Nodes[j];
                            PosH = treeListView1.Nodes.IndexOf(treeListView1.Nodes[j]);
                            gt = i;
                            if (TTT.Nodes.Count >= 1 )
                            {
                                treeListView1.Nodes[PosH].Nodes[i].Remove(); 
                            }
//                            if (TTT.Text.ToLower() == "explorer.exe" & TTT.SubItems[7].Text != "0")
//                            {
//                                treeListView1.Nodes[PosH].Nodes[i].Remove();
//                            }

                        }
                        
                    }
                }
//                    for (int i = 0; i < treeListView1.Nodes[treeListView1.Nodes.IndexOf(treeListView1.GetItemAt(s))].Nodes.Count; i++)
//                    {
//                        if (treeListView1.Nodes[treeListView1.Nodes.IndexOf(treeListView1.GetItemAt(s))].Nodes[i].SubItems[2].Text == Handle.ToString())
//                        {
//                            TTT = treeListView1.Nodes[treeListView1.Nodes.IndexOf(treeListView1.GetItemAt(s))];
//                            PosH = treeListView1.Nodes.IndexOf(treeListView1.GetItemAt(s));
//
//                            treeListView1.Nodes[treeListView1.Nodes.IndexOf(treeListView1.GetItemAt(s))].Nodes[i].Remove();
//
//                        }
//
//                    }

                H1 = (IntPtr)Convert.ToInt32(treeListView1.Nodes[PosH].SubItems[0].Text);

                H2 = (IntPtr)Convert.ToInt32(treeListView1.Nodes[PosH].SubItems[1].Text);
                PerPixelAlphaForm HH1 = null;
                if (H1 != IntPtr.Zero)
                {
                    HH1 = FromHandle(H1) as PerPixelAlphaForm; 
                }
                Bitmap B;
                Bitmap Buttonbmp = new Bitmap(ButtonBmpS);
                Bitmap Buttonbmp2 = new Bitmap(ButtonBmpS2);
                Bitmap Buttonbmp3 = new Bitmap(ButtonBmpS3);
                if (treeListView1.Nodes[PosH].Nodes.Count == 1)
                {
//                    Bitmap TransBgr = new Bitmap(TransbgrS);
                    B =  new Bitmap(15, 15);
                    B.MakeTransparent();
//                    TransBgr.Dispose();
//                    TransBgr = null;
                    GC.Collect();
                }
                else
                {
                    Bitmap CountBadge = new Bitmap(CountBadgeS);
                    B = new Bitmap(CountBadge, 15, 15); 
                    CountBadge.Dispose();
                    CountBadge = null;
                    GC.Collect();
                }
                Graphics g = Graphics.FromImage(B);
                g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
                if (treeListView1.Nodes[PosH].Nodes.Count == 1)
                {
                    g.DrawString("", new Font("Tahoma", 8, FontStyle.Bold), Brushes.Black, new PointF(0, 0));
                }
                else
                {
                    g.DrawString((treeListView1.Nodes[PosH].Nodes.Count).ToString(),
                                 new Font("Tahoma", 8, FontStyle.Bold), Brushes.Black, new PointF(2, 0));
                }
                int Pined = 0;
                try
                {
                    Pined = Convert.ToInt32(treeListView1.Nodes[PosH].SubItems[7].Text);
                }
                catch
                {

                    Pined = 0;
                }

                if (Pined != 1)
                {
                    try
                    {
                        var FP = FromHandle((IntPtr)Convert.ToInt32(treeListView1.Nodes[PosH].SubItems[5].Text)) as PerPixelAlphaForm;
                        FP.SetBitmap(B);
                        if (CountBadgeBool == 0)
                        {
                            WindowsAPI.ShowWindow(Convert.ToInt32(treeListView1.Nodes[PosH].SubItems[5].Text), 1);
                        }
                    }
                    catch 
                    {

                    }
                }
                else
                {
                    if (treeListView1.Nodes[PosH].Nodes.Count <= 1)
                    {
                        try
                        {
                            WindowsAPI.ShowWindow(Convert.ToInt32(treeListView1.Nodes[PosH].SubItems[5].Text), 0);
                        }
                        catch
                        {

                        } 
                    }
                    else
                    {
                        try
                        {
                            var FP = FromHandle((IntPtr)Convert.ToInt32(treeListView1.Nodes[PosH].SubItems[5].Text)) as PerPixelAlphaForm;
                            FP.SetBitmap(B);
                            if (CountBadgeBool == 0)
                            {
                                WindowsAPI.ShowWindow(Convert.ToInt32(treeListView1.Nodes[PosH].SubItems[5].Text), 1);
                            }
                        }
                        catch
                        {

                        }
                    }
                }

                B.Dispose();
                g.Dispose();

                if (H1 != IntPtr.Zero & H2 != IntPtr.Zero)
                {
//                    treeListView1.Nodes[PosH].Nodes[gt].Remove();
                    
                    if (TTT.Nodes.Count == 0)
                    {
//                        if (TTT.SubItems[7].Text == "0")
//                        {
                        if (Pined != 1)
                        {
                            var BF = FromHandle(H1) as Form;
                            try
                            {
                                BF.OwnedForms[0].OwnedForms[0].Dispose();
                            }
                            catch
                            {

                            }
                            try
                            {
                                //WindowsAPI.ShowWindow((int)BF.OwnedForms[0].Handle, 0);
                                BF.OwnedForms[0].Dispose();
                            }
                            catch
                            {

                            }
                            BF.Close();
                            for (int i = PosH; i < treeListView1.Nodes.Count - 1; i++)
                            {
                                Form MF;
                                Form MIF;
                                Form MC;

                                MF = FromHandle((IntPtr)Convert.ToInt32(treeListView1.Nodes[i + 1].SubItems[0].Text)) as Form;

                                MF.Location = new Point(MF.Location.X - MF.Width - 2, MF.Location.Y);
                                
                                MIF = FromHandle((IntPtr)Convert.ToInt32(treeListView1.Nodes[i + 1].SubItems[1].Text)) as Form;

                                MIF.Location = new Point(MIF.Location.X - MF.Width - 2, MIF.Location.Y);

                                try
                                {
                                    MC = FromHandle((IntPtr)Convert.ToInt32(treeListView1.Nodes[i + 1].SubItems[5].Text)) as Form;

                                    MC.Location = new Point(MC.Location.X - MF.Width - 2, MC.Location.Y);
                                }
                                catch
                                {

                                }
                            }

                            treeListView1.Nodes[PosH].Remove(); 
                        }
                        else
                        {
                            PerPixelAlphaForm F =
                                FromHandle(H1) as
                                PerPixelAlphaForm;
                            Bitmap ButtonBmpT = new Bitmap(ButtonBmpS);
                            Bitmap ButtonBmp = new Bitmap(TransS);
                            Bitmap QL = new Bitmap(ButtonBmp, ButtonBmpT.Width, ButtonBmpT.Height);
                            ButtonBmpT.Dispose();
                            ButtonBmp.Dispose();
                            F.SetBitmap(QL);

                        }



                    }
                    else if (TTT.Nodes.Count == 1)
                    {
                        if (HH1 != null)
                        {
                            HH1.SetBitmap(Buttonbmp);
                        }
                    }
                    else if (TTT.Nodes.Count == 2)
                    {
                        if (HH1 != null)
                        {
                            HH1.SetBitmap(Buttonbmp2);
                        }
                    }
                    else if (TTT.Nodes.Count > 2)
                    {
                        if (HH1 != null)
                        {
                            HH1.SetBitmap(Buttonbmp3);
                        }
                    }
                    
                }
                if (sv.OSMajorVersion != 6)
                {
                    var f = new FileInfo(ExePath + "\\Thumb\\" + Handle + ".png");
                    f.Delete(); 
                }
                
            }
            catch
            {
                
            }
//	        WindowsAPI.SetForegroundWindow(IntPtr.Zero);
//            WindowsAPI.SetActiveWindow(IntPtr.Zero);
//	        Foreground = IntPtr.Zero;
            
		}

		private void ShellRedraw(IntPtr Handle)
		{
//            string s = Path.GetFileName(WindowsAPI.GetWindowModuleFileName(Handle)).ToLower();
//
//		    if (WindowsAPI.GetForegroundWindow() != Handle)
//            {
//                TreeListNode T = null;
//                try
//                {
//                    for (int i = 0; i < treeListView1.Nodes[treeListView1.Nodes.IndexOf(treeListView1.GetItemAt(s))].Nodes.Count; i++)
//                    {
//                        if (treeListView1.Nodes[treeListView1.Nodes.IndexOf(treeListView1.GetItemAt(s))].Nodes[i].SubItems[2].Text == Handle.ToString())
//                        {
//                            T = treeListView1.Nodes[treeListView1.Nodes.IndexOf(treeListView1.GetItemAt(s))].Nodes[i];
//                        }
//                    }
//                    if ((T.SubItems[4].Text == "0") | (DateTime.Now.TimeOfDay - TimeSpan.Parse(T.SubItems[4].Text) > TimeSpan.Parse("00:00:04.50")))
//                    {
//                        T.SubItems[4].Text = DateTime.Now.TimeOfDay.ToString();
//                        T.SubItems[3].Text = "0";
//                    }
//
//                    if (DateTime.Now.TimeOfDay - TimeSpan.Parse(T.SubItems[4].Text) < TimeSpan.Parse("00:00:03.00"))
//                    {
//                       T.SubItems[3].Text = (Convert.ToInt32(T.SubItems[3].Text) + 1).ToString(); 
//                    }
// 
//                    if (Convert.ToInt32(T.SubItems[3].Text)> 2)
//                    {
//                        try
//                        {
//                            var F = FromHandle((IntPtr)Convert.ToInt32(treeListView1.Nodes[treeListView1.Nodes.IndexOf(treeListView1.GetItemAt(s))].SubItems[0].Text)) as PerPixelAlphaForm;
//                            bool ISThereTimer = false;
//                            foreach (Timer t in Timers)
//                            {
//                                if ((IntPtr)t.Tag == F.Handle)
//                                {
//                                    ISThereTimer = true;
//                                }
//                            }
//                            F.SetBitmap(ButtonFlash);
//                            if (!ISThereTimer)
//                            {
//                                var T1 = new Timer();
//                                var T2 = new Timer();
//                                Timers.Add(T1);
//                                Timers.Add(T2);
//                                T1.Tag = F.Handle;
//                                T2.Tag = F.Handle;
//                                T1.Interval = 1000;
//                                T2.Interval = 1500;
//                                T1.Tick += T1_Tick;
//                                T2.Tick += T2_Tick;
//                                T1.Enabled = true;
//                                T2.Enabled = true; 
//                            }
//                            F.Tag = "Flashing";
//                        }
//                        catch
//                        {
//                            
//
//                        }  
//                    }
//
//                }
//                catch
//                {
//                    
//
//                }
//                
//            }
		}

        void T2_Tick(object sender, EventArgs e)
        {
            try
            {
                var F = FromHandle((IntPtr)((sender as Timer).Tag)) as PerPixelAlphaForm;
                TreeListNode TTT = null;
                for (int i = 0; i < treeListView1.Nodes.Count; i++)
                {
                    if (treeListView1.Nodes[i].SubItems[0].Text == F.Handle.ToString())
                    {
                        TTT = treeListView1.Nodes[i];
                    }

                }
                Bitmap ButtonBmp = new Bitmap(ButtonBmpS);
                Bitmap ButtonBmp2 = new Bitmap(ButtonBmpS2);
                Bitmap ButtonBmp3 = new Bitmap(ButtonBmpS3);
                if (TTT.Nodes.Count == 2)
                {
                    F.SetBitmap(ButtonBmp2);
                }
                else if (TTT.Nodes.Count > 2)
                {
                    F.SetBitmap(ButtonBmp3);
                }
                else if (TTT.Nodes.Count > 0)
                {
                    F.SetBitmap(ButtonBmp);
                }
                ButtonBmp2.Dispose();
                ButtonBmp3.Dispose();
//                F.SetBitmap(ButtonBmp);
                ButtonBmp.Dispose();
                ButtonBmp = null;
            }
            catch
            {
                
            }
        }

        void T1_Tick(object sender, EventArgs e)
        {
            try
            {
                var F = FromHandle((IntPtr)((sender as Timer).Tag)) as PerPixelAlphaForm;
                TreeListNode TTT = null;
                for (int i = 0; i < treeListView1.Nodes.Count; i++)
                {
                    if (treeListView1.Nodes[i].SubItems[0].Text == F.Handle.ToString())
                    {
                        TTT = treeListView1.Nodes[i];
                    }

                }
                if (TTT.Nodes.Count == 2)
                {
                    F.SetBitmap(ButtonFlash2);
                }
                else if (TTT.Nodes.Count > 2)
                {
                    F.SetBitmap(ButtonFlash3);
                }
                else
                {
                    F.SetBitmap(ButtonFlash);
                }
//                F.SetBitmap(ButtonFlash);
            }
            catch
            {

            }
        }

	    private void SetHook()
        {
//            mgr = new SpyMgr();
////            mgr.set_ReportProcessCreation(DeviareCommonLib.ReportMethod._create_process_hook_and_polling, 0);
////	        mgr.DebugPrivileges = 1;
//            DeviareTools.IProcesses procs = mgr.get_Processes(0);
////                DeviareTools.IProcesses procs2 = mgr2.get_Processes(0);
//            hook = mgr.CreateHook("user32.dll!FlashWindowEx");
//	        
//            hook.Attach(procs);
//                
//            _proxy = mgr.CreateHooksEventsProxy() as Deviare.HooksEventsProxy;
//            _proxy.OnFunctionCalled += new DHooksEvents_OnFunctionCalledEventHandler(_proxy_OnFunctionCalled);
//            _proxy.Attach(mgr.Hooks);
////            hook2.OnFunctionCalled += new DHookEvents_OnFunctionCalledEventHandler(hook2_OnFunctionCalled);
//            hook.AddProcessFilter(1,"SBar.exe",0);
//            hook.AddProcessFilter(1, "FlashTest.exe", 0);
//            hook.AddProcessFilter(1, "FlashTest", 0);
//            hook.AddProcessFilter(1, "skypePM.exe", 0);
////            hook.AddProcessFilter(1, "skypePM", 0);
//            hook.Properties = (int)DeviareCommonLib.NktHookFlags._call_before;
////            hook.set_HookNewProcesses(0, 1);
//            hook.Hook();
////            _proxy2 = mgr.CreateComputerProcessesEventsProxy() as ComputerProcessesEventsProxy;
////            _proxy2.Attach(procs);
////            _proxy2.OnProcessTerminated += new DComputerProcessesEvents_OnProcessTerminatedEventHandler(_proxy2_OnProcessTerminated);
////            _proxy2.OnProcessStarted += new DComputerProcessesEvents_OnProcessStartedEventHandler(_proxy2_OnProcessStarted);
//                       
//            
        }

	    private   bool BBB = true;


        private   void GenerateFolderData()
        {
            //1. the top level keyValue data of RecentDocs
            RecentDocsFormatToplevel = new RecentDocsFormat(RecentKey);

            //2. generate the set of subkeys under RecentDocs
            RecentDocsSubkeys = new Dictionary<string, RecentDocsFormat>();

            string[] sList = RecentKey.GetSubKeyNames();
            foreach (string sreg in sList)
            {
                RegistryKey subKey = RecentKey.OpenSubKey(sreg, true);
                RecentDocsFormat folder = new RecentDocsFormat(subKey);

                // The Add method throws an exception if the new key is 
                // already in the dictionary.
                try
                {
                    RecentDocsSubkeys.Add(sreg, folder);
                }
                catch (ArgumentException)
                {
                    Console.WriteLine("An element with Key = {0} already exists.", sreg);
                }
            }
        }
        private   void PopulateListview()
        {
//            //GenerateFolderData();
//            listView1.Items.Clear();
//            Items.Clear();//clear display
//            ItemsFirefox.Clear();


        }
        private int MaxTextWidth = 0;
      
        private   void DisplayFolderStats()
        {
            int folderEntries = 0;
            if (FolderKey != null)
            {
                List<string> sKeyValuesRecent = RegTools.ExtractKeyValues(RecentKey);
                List<string> sKeyValuesFolder = RegTools.ExtractKeyValues(FolderKey);
                if (RecentKey != null)
                {
                    foreach (String s in sKeyValuesRecent)
                    {
                        object keyObj = RecentKey.GetValue(s, typeof(string));
                        byte[] Bytes = (byte[])keyObj;

                        foreach (String sFold in sKeyValuesFolder)
                        {
                            object robjFold = FolderKey.GetValue(sFold, typeof(string));
                            if (RegTools.IsBinaryMatch((byte[])keyObj, (byte[])robjFold))
                            {
                                folderEntries++;
                                break;
                            }
                        }
                    }
                }
            }
            //write to status bar
        }


        public static WindowsAPI.TaskBarState GetTaskBarState()
        {
            WindowsAPI.APPBARDATA appBar = WindowsAPI.CreateAppBarData();

            return (WindowsAPI.TaskBarState)WindowsAPI.SHAppBarMessage(WindowsAPI.ABMsg.ABM_GETSTATE, ref appBar);
        }

	    public PerPixelAlphaForm F1 = null;
	    public int TaskHeight = 0;
	    public int TaskWidth = 0;
        private void Form1_Load(object sender, EventArgs e)
        {

            WindowsAPI.TaskBarState TS = GetTaskBarState();

            if (sv.OSVersion != OSVersion.Vista)
            {
                
                SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(SystemEvents_PowerModeChanged); 
            }
            if (!File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\SbarDB.mdb"))
            {
               File.Copy(ExePath + @"\SBar.mdb",Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\SbarDB.mdb"); 
            }
            if (File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), "SBar.lnk")))
            {
                File.Copy(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), "SBar.lnk"),Path.GetDirectoryName(Application.ExecutablePath) + @"\SBar.lnk",true);
            }
            csSbar.Close();
            csSbar.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "\"" + Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\SbarDB.mdb" + "\"";//""E:\Personal Projects\C#\Global Hook\GlobalHooksTest\bin\Debug\SbarDB.mdb"
            csSbar.Open();
           
            taskbarPinnedTableAdapter.Connection = csSbar;
            pinnedTableAdapter1.Connection = csSbar;
            recentTableAdapter.Connection = csSbar;
            exclusionTableAdapter1.Connection = csSbar;
            // TODO: This line of code loads data into the 'ds1.TaskbarPinned' table. You can move, or remove it, as needed.
            this.taskbarPinnedTableAdapter.Fill(this.ds1.TaskbarPinned);
            this.exclusionTableAdapter1.Fill(this.ds1.Exclusion);
            // TODO: This line of code loads data into the 'ds1.Recent' table. You can move, or remove it, as needed.
            this.recentTableAdapter.Fill(this.ds1.Recent);
            ExclusionList.Clear();
            foreach (DataRow dr in ds1.Exclusion.Rows)
            {

                ExclusionList.Add(dr["PATH"].ToString().ToLowerInvariant());
            }
            trPop.Stop();
            try
            {
                
                // TODO: This line of code loads data into the 'ds1.RecentDoc' table. You can move, or remove it, as needed.
                //            this.recentDocTableAdapter.Fill(this.ds1.RecentDoc);
                SettingsIni = new IniFile(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\SBar.ini");
                
                sqliteSelectComm = sqliteSelectCommand1;
                sqLiteConn = sqLiteConnection1;
                sqLiteDataA = sqLiteDataAdapter1;
                SBarDS = ds1;
                CurrentUserKey = Registry.CurrentUser;
                RecentKey = CurrentUserKey.OpenSubKey(sRecentKey, true);
                FolderKey = CurrentUserKey.OpenSubKey(sFolderKey, true);
                MyRecentDocsFolder = Environment.GetFolderPath(Environment.SpecialFolder.Recent);

                if (!File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\SBar.ini"))
                {
//                  File.CreateText(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\SBar.ini");  
                }
                if (File.Exists(Path.GetDirectoryName(Application.ExecutablePath) + @"\WebUpdate.exe"))
                {
                    miUpdateCheck.Enabled = true;
                }
                else
                {
                    miUpdateCheck.Enabled = false;
                }
//                Process[] pp = Process.GetProcesses();
//                proclst.AddRange(pp);
                SetHook();
                ExePath = Path.GetDirectoryName(Application.ExecutablePath);

                //            Timer2.Interval = 1;
                //            Timer2.Tick += timer2_Tick;
                //            Timer2.Enabled = true;
                //            Timer2.Stop();

                var iniDesktop = new WindowsAPI.RECT();
                IntPtr ptr = IntPtr.Zero;

                ptr = Marshal.AllocHGlobal(Marshal.SizeOf(iniDesktop));

                Marshal.StructureToPtr(iniDesktop, ptr, true);

                WindowsAPI.SystemParametersInfo(WindowsAPI.SPI.SPI_GETWORKAREA, (uint)Marshal.SizeOf(iniDesktop), ptr, 0);

                iniDesktop = (WindowsAPI.RECT)Marshal.PtrToStructure(ptr, new WindowsAPI.RECT().GetType());
                Desktop = iniDesktop;
                WindowsAPI.RECTW TaskPos = new WindowsAPI.RECTW();
                WindowsAPI.GetWindowRect(hReBar, ref TaskPos);
                WindowsAPI.RECTW TaskHPos = new WindowsAPI.RECTW();
                WindowsAPI.GetWindowRect(hTask, ref TaskHPos);
                WindowsAPI.RECTW QLPos = new WindowsAPI.RECTW();
                WindowsAPI.GetWindowRect(hQuickLaunch, ref QLPos);
                TaskbarPosY = TaskPos.Top;
                TaskHeight = TaskHPos.Height;
                TaskWidth = TaskHPos.Width;
                tmRefreshDropZone.Start();
                if (SettingsIni.IniReadValue("Main", "XPos") != "" & SettingsIni.IniReadValue("Main", "XPos") != null)
                {
                    P.X = Convert.ToInt32(SettingsIni.IniReadValue("Main", "XPos"));
                }
                else
                {
                    if (hQuickLaunch != IntPtr.Zero)
                    {
                        if (TaskPos.Top < TaskHPos.Top)
                        {
                            P.X = TaskPos.Left; 
                        }
                        else
                        {
                            if (QLPos.Left < TaskHPos.Left)
                            {
                                P.X = TaskHPos.Left; 
                            }
                            else
                            {
                                P.X = TaskPos.Left; 
                            }
                        }
                    }
                    else
                    {
                        P.X = TaskPos.Left; 
                    }
                    Taskbarhide = true;
                    SettingsIni.IniWriteValue("Main", "XPos", P.X.ToString());
                    SettingsIni.IniWriteValue("Main", "TaskbarHide", Convert.ToInt32(Taskbarhide).ToString());
                }
                if (SettingsIni.IniReadValue("Main", "YPos") != "" & SettingsIni.IniReadValue("Main", "YPos") != null)
                {

//                        P.Y = TaskHPos.Top;
                    
                        P.Y = Convert.ToInt32(SettingsIni.IniReadValue("Main", "YPos")); 
                    if (TaskPos.Top > P.Y )
                    {
                        P.Y = TaskPos.Top; 
                    }
                }
                else
                {
//                    P.Y = Desktop.Bottom - 0 + 2;
                    if (hQuickLaunch != IntPtr.Zero)
                    {
                        if (TaskPos.Top < TaskHPos.Top)
                        {
                            P.Y = QLPos.Location.Y + QLPos.Height;
                        }
                        else
                        {
                            P.Y = TaskPos.Location.Y;
                        }
//                        P.Y = QLPos.Location.Y + QLPos.Height; 
                    }
                    else
                    {
                        P.Y = TaskPos.Location.Y; 
                    }
                    SettingsIni.IniWriteValue("Main", "YPos", P.Y.ToString());
                }
                if (Taskbarhide)
                {
                    WindowsAPI.ShowWindow((int)hTask, 0);
                }
                if (SettingsIni.IniReadValue("Main", "Count") == null | SettingsIni.IniReadValue("Main", "Count") == "")
                {
                    CountBadgeBool = 0;
                    SettingsIni.IniWriteValue("Main", "Count", "0");
                }
                Pr.X = P.X + 13;
                Pr.Y = P.Y + 2;
                var rrr = new WindowsAPI.RECT();
                InitialareaX = rrr.Top;
                InitialAreaY = rrr.Bottom;
                Startbar.ShowInTaskbar = false;
                Startbar.SetBitmap(StartBarBackgroubd,0);
                Startbar.Location = new Point(0, Desktop.Bottom - 0);
                Startbar.TopMost = true;
                Startbar.Show(this);
                if (sv > OSVersionInfo.WinXP)
                {
                    VistaStart = WindowsAPI.FindWindowEx(IntPtr.Zero, IntPtr.Zero, (IntPtr)0xC017, "Start");
                    WindowsAPI.SetParent(Startbar.Handle, VistaStart);
                }
                else
                {
                    WindowsAPI.SetParent(Startbar.Handle, hTask);
                }
                F1 = new PerPixelAlphaForm();
                F1.ShowInTaskbar = false;
                F1.TopMost = true;
                F1.Name = "DropForm";
                F1.ContextMenu = mnuTaskbarRightClick;
                F1.Activated += new EventHandler(F1_Activated);
                F1.DragEnter += new DragEventHandler(F1_DragEnter);
                F1.DragDrop += new DragEventHandler(F1_DragDrop);
                F1.AllowDrop = true;
                Bitmap GHJ = new Bitmap(StartBarBackgroubd,TaskHPos.Width,TaskHPos.Height);
                F1.SetBitmap(GHJ,25);
                F1.Location = new Point(TaskHPos.Left,TaskHPos.Top);
                F1.Show(Startbar);
                
                //            WindowsAPI.SetParent(Startbar.Handle, hTask);
                SetValues();
                GetTaskbarPinned();
                Bitmap SizeBmp = new Bitmap(ButtonBmpS);
                if (treeListView1.Nodes.Count > 0)
                {

                    PX = P.X + (SizeBmp.Width + 2) * treeListView1.Nodes.Count;

                }
                else
                {
                    PX = P.X;
                }
                SizeBmp.Dispose();
                GetWindows();
                FillButtons();
                WindowsAPI.ShowWindow((int)Handle, (int)WindowsAPI.SW.HIDE);
                
                Startbar.TopMost = true;
//                WindowsAPI.SetActiveWindow(IntPtr.Zero);
                pinnedTableAdapter1.Fill(ds1.Pinned);
                ShowDesktop = new PerPixelAlphaForm();
                ShowDesktop.Name = "ShowDesktop";
                ShowDesktop.StartPosition = FormStartPosition.Manual;
                Bitmap ShowdesktopBmpTemp = new Bitmap(ButtonShowDesktopS);
                Bitmap ShowdesktopBmp = new Bitmap(ShowdesktopBmpTemp, ShowdesktopBmpTemp.Width,TaskPos.Height);
                ShowdesktopBmpTemp.Dispose();
                ShowDesktop.Location = new Point(Screen.PrimaryScreen.WorkingArea.Right - ShowdesktopBmp.Width, TaskPos.Top);
                ShowDesktop.TopMost = true;
                ShowDesktop.MouseEnter += ShowDesktop_MouseHover;
                ShowDesktop.MouseLeave += ShowDesktop_MouseLeave;
                ShowDesktop.Click += new EventHandler(ShowDesktop_Click);
                ShowDesktop.ShowInTaskbar = false;
                ShowDesktop.Show(Startbar);
                ShowDesktop.SetBitmap(ShowdesktopBmp);
                ShowdesktopBmp.Dispose();
                GC.Collect();
                FlushMemory();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            miLockTaskbar.Checked = true;
            WindowsAPI.SetForegroundWindow(IntPtr.Zero);
//            WindowsAPI.SetActiveWindow(IntPtr.Zero);
            FileSystemWatcher watcher = new FileSystemWatcher();
            watcher.Filter = "*.lnk";
            watcher.Created += new FileSystemEventHandler(watcher_Created);
            watcher.Changed += new FileSystemEventHandler(watcher_Changed);
            
            //Set the path to C:\Temp\
            watcher.Path = Environment.GetFolderPath(Environment.SpecialFolder.Recent);

            //Enable the FileSystemWatcher events.
            watcher.EnableRaisingEvents = true;
            RecDoc.Clear();
            listView1.Items.Clear();
            System.IO.DirectoryInfo Recdir =
                new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Recent));
            foreach (FileInfo file in Recdir.GetFiles())
            {
                ListViewItem lv = new ListViewItem(file.FullName);

                lv.SubItems.Add(File.GetCreationTimeUtc(file.FullName).ToFileTimeUtc().ToString()); 
                
                listView1.Items.Add(lv);
//                RecDoc.Add(file.FullName);
            }
//            listView1.Sort();
            listViewExSort1.SortColumn = 1;
            foreach (ListViewItem lvi in listView1.Items)
            {
                RecDoc.Add(lvi.Text);
            }


            IntPtr procHandle = WindowsAPI.GetCurrentProcess();
            WindowsAPI.SetPriorityClass(procHandle, (uint)WindowsAPI.DWPriorityClass.HIGH_PRIORITY_CLASS); 
            
        }

        void F1_DragDrop(object sender, DragEventArgs e)
        {
            string[] FileList = (string[])e.Data.GetData(DataFormats.FileDrop, false);

            // Do something with the data...

            // For example add all files into a simple label control:
            foreach (string File in FileList)
            {
                if (Path.GetExtension(File).ToLowerInvariant() == ".exe")
                {
                    int MAx = 1;
                    try
                    {
                        MAx = (int)taskbarPinnedTableAdapter.MAXCODE() + 1;
                    }
                    catch
                    {
                        MAx = 1;
                    }
                    taskbarPinnedTableAdapter.Insert(Path.GetFileName(File).ToLower(), File, MAx);
                    taskbarPinnedTableAdapter.Update(ds1.TaskbarPinned);
                    ds1.AcceptChanges();

                    //------------
                    string s = Path.GetFileName(File).ToLower();
                    //                ContainerListViewObject cv = treeListView1.GetItemAt(s);

                    var t = new PerPixelAlphaForm();
                    var tt = new PerPixelAlphaForm();
                    var tc = new PerPixelAlphaForm();
                    var tln = new TreeListNode(s);
                    //                            var sub = new TreeListNode(WindowsAPI.GetWindowName(w.Handle));
                    t.ShowInTaskbar = false;
                    tc.ShowInTaskbar = false;
                    tt.ShowInTaskbar = false;
                    //                t.Text = w.Handle.ToString();
                    t.TopMost = true;
                    tt.TopMost = true;
                    tc.TopMost = true;
                    t.AllowDrop = true;
                    tt.AllowDrop = true;
                    t.MouseEnter += t_MouseHover;
                    t.MouseHover += t_MouseHover_Real;
                    t.MouseLeave += t_MouseLeave;
                    tt.MouseEnter += tt_MouseHover;
                    tt.MouseHover += tt_MouseHover_Real;
                    tt.MouseLeave += tt_MouseLeave;
                    t.DragEnter += new DragEventHandler(t_DragEnter);
                    t.DragDrop += new DragEventHandler(t_DragDrop);
                    tt.DragDrop += t_DragDrop;
                    tt.DragEnter += t_DragEnter;
                    t.MouseUp += t_new_Click;
                    tt.MouseUp += tt_MouseUp;
                    //                            tt.Text = w.Handle.ToString();
                    Bitmap ButtonBmpT = new Bitmap(ButtonBmpS);
                    Bitmap ButtonBmp = new Bitmap(TransS);
                    Bitmap QL = new Bitmap(ButtonBmp, ButtonBmpT.Width, ButtonBmpT.Height);
                    ButtonBmpT.Dispose();
                    t.SetBitmap(QL);
                    int ButtonWidth = QL.Width;
                    int ButtonHeight = QL.Height;
                    Icon I = null;
                    if (ButtonWidth > 59)
                    {
                        I = lst.Icon(lst.IconIndex(File));
                    }
                    else
                    {
                        I = lsts.Icon(lsts.IconIndex(File));
                    }
                    ButtonBmp.Dispose();
                    ButtonBmp = null;
                    QL.Dispose();
                    GC.Collect();
                    Bitmap Explorer = new Bitmap(ExePath + "\\Skins\\" + SkinDir + "\\explorer32.png");
                    if (s.ToLower() == "explorer.exe")
                    {
                        tt.SetBitmap(Explorer);
                        Explorer.Dispose();
                        Explorer = null;
                    }
                    else
                    {
                        tt.SetBitmap(I.ToBitmap());
                        Explorer.Dispose();
                        Explorer = null;
                    }
                    double E = (ButtonWidth + 2) / 1.5;
                    PerPixelAlphaForm LastBForm =
                        FromHandle((IntPtr)Convert.ToInt32(treeListView1.Nodes[treeListView1.Nodes.Count - 1].SubItems[0].Text))
                        as PerPixelAlphaForm;
                    t.Location = new Point(LastBForm.Location.X + (ButtonWidth + 2), P.Y);
                    //                            tt.Location = new Point(Pr.X + (ButtonWidth + 2) * j++, P.Y + (ButtonHeight - I.Height)/2);
                    tt.Location = new Point(t.Location.X + (ButtonWidth - I.Width) / 2, P.Y + (ButtonHeight - I.Height) / 2);
                    //                tc.Location = new Point(t.Location.X + (int)E, t.Location.Y + (int)E / 2);
                    t.Show(Startbar);
                    tt.Show(t);
                    tln.SubItems.Add(t.Handle.ToString());
                    tln.SubItems.Add(tt.Handle.ToString());
                    tln.SubItems.Add("");
                    tln.SubItems.Add("");
                    tln.SubItems.Add("");
                    tln.SubItems.Add("");
                    tln.SubItems.Add(File);
                    tln.SubItems.Add("1");
                    treeListView1.Nodes.Add(tln);
                    I.Dispose();
                    I = null;
                    GC.Collect(); 
                }
                //----------

            }
        }

        void F1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Copy; // Okay
            }
            else
                e.Effect = DragDropEffects.None;
        }

        void F1_Activated(object sender, EventArgs e)
        {
//            WindowsAPI
            foreach (Form FF in Startbar.OwnedForms)
            {
               FF.BringToFront();
               FF.TopMost = true;
            }
        }

        void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
        {



            Bitmap ButtonBmp = new Bitmap(ButtonBmpS);
            Bitmap ButtonBmp2 = new Bitmap(ButtonBmpS2);
            Bitmap ButtonBmp3 = new Bitmap(ButtonBmpS3);
            Bitmap ButtonPinned = new Bitmap(ButtonQLH);
            Bitmap DesktopSWTemp = new Bitmap(ButtonShowDesktopS);
            WindowsAPI.RECTW TaskPos = new WindowsAPI.RECTW();
            WindowsAPI.GetWindowRect(hReBar, ref TaskPos);
            Bitmap DesktopSW = new Bitmap(DesktopSWTemp, DesktopSWTemp.Width, TaskPos.Height);
	           
            foreach (Form FForm in Startbar.OwnedForms)
            {
                if (FForm.Name == "ShowDesktop")
                {
                    (FForm as PerPixelAlphaForm).SetBitmap(DesktopSW);
                }
                if (FForm.Name == "DropForm")
                {
                    Bitmap GHJ = new Bitmap(StartBarBackgroubd, TaskPos.Width - 150, TaskPos.Height * 2);
                    (FForm as PerPixelAlphaForm).SetBitmap(GHJ,25);
                    GHJ.Dispose();
                }
            }
            DesktopSW.Dispose();
            for (int i = 0; i < treeListView1.Nodes.Count; i++)
            {
                Icon II = null;
                try
                {
                    
                    if (ButtonBmp.Width > 54)
                    {
                        if (!File.Exists(ExePath + "\\Skins\\" + SkinDir + "\\" + Path.GetFileNameWithoutExtension(treeListView1.Nodes[i].SubItems[6].Text) + ".png"))
                        {
                            II = lst.Icon(lst.IconIndex(treeListView1.Nodes[i].SubItems[6].Text));
                        }
                        else
                        {
                            Bitmap IconBMP = new Bitmap(ExePath + "\\Skins\\" + SkinDir + "\\" + Path.GetFileNameWithoutExtension(treeListView1.Nodes[i].SubItems[6].Text) + ".png");
                            II = BitmapToIcon(IconBMP);
                            IconBMP.Dispose();
                        }
                    }
                    else
                    {
                        if (!File.Exists(ExePath + "\\Skins\\" + SkinDir + "\\" + Path.GetFileNameWithoutExtension(treeListView1.Nodes[i].SubItems[6].Text) + ".png"))
                        {
                            II = lsts.Icon(lsts.IconIndex(treeListView1.Nodes[i].SubItems[6].Text));
                        }
                        else
                        {
                            Bitmap IconBMP = new Bitmap(ExePath + "\\Skins\\" + SkinDir + "\\" + Path.GetFileNameWithoutExtension(treeListView1.Nodes[i].SubItems[6].Text) + ".png");
                            II = BitmapToIcon(IconBMP);
                            IconBMP.Dispose();
                        }
                    }
                    //                    II = lsts.Icon(lsts.IconIndex(treeListView1.Nodes[i].SubItems[6].Text));
                }
                catch
                {

                }
                PerPixelAlphaForm FPForm =
                            FromHandle((IntPtr)Convert.ToInt32(treeListView1.Nodes[i].SubItems[0].Text)) as
                            PerPixelAlphaForm;
                if (II != null & Path.GetFileNameWithoutExtension(treeListView1.Nodes[i].SubItems[6].Text).ToLower() != "explorer")
                {

                    (FPForm.OwnedForms[0] as PerPixelAlphaForm).SetBitmap(II.ToBitmap());
                }
                else if (Path.GetFileNameWithoutExtension(treeListView1.Nodes[i].SubItems[6].Text).ToLower() == "explorer")
                {
                    Bitmap ExplorerBM = new Bitmap(ExePath + "\\Skins\\" + SkinDir + "\\explorer32.png");
                    (FPForm.OwnedForms[0] as PerPixelAlphaForm).SetBitmap(ExplorerBM);
                    //                            Explorer.Dispose();
                }

                if (treeListView1.Nodes[i].Nodes.Count >= 3)
                {
                    FPForm.SetBitmap(ButtonBmp3);
                }
                else if (treeListView1.Nodes[i].Nodes.Count == 2)
                {
                    FPForm.SetBitmap(ButtonBmp2);
                }
                else if (treeListView1.Nodes[i].Nodes.Count == 1)
                {
                    FPForm.SetBitmap(ButtonBmp);
                }
                else if (treeListView1.Nodes[i].Nodes.Count < 1)
                {
                    FPForm.SetBitmap(ButtonPinned, 10);
                }

            }
            //            foreach (Form form in Startbar.OwnedForms)
            //            {
            //                WindowsAPI.SetWindowPos(form.Handle, WindowsAPI.HWND_TOPMOST, 0, 0, 0, 0,
            //                                        WindowsAPI.SWP_NOMOVE | WindowsAPI.SWP_NOSIZE | WindowsAPI.SWP_NOACTIVATE);
            //            }
            Startbar.TopMost = true;
            ButtonBmp.Dispose();
            ButtonBmp2.Dispose();
            ButtonBmp3.Dispose();
            ButtonPinned.Dispose();
        }

        void ShowDesktop_Click(object sender, EventArgs e)
        {
            WindowsAPI.Shell shell = new WindowsAPI.Shell();
            if (IsPushMinimize)
            {
                shell.UndoMinimizeALL();
                IsPushMinimize = false;
            }
            else
            {
                shell.MinimizeAll();
                IsPushMinimize = true;
            }

        }

        void ShowDesktop_MouseLeave(object sender, EventArgs e)
        {
            WindowsAPI.RECTW TaskPos = new WindowsAPI.RECTW();
            WindowsAPI.GetWindowRect(hReBar, ref TaskPos);
            Bitmap ShowdesktopBmpTemp = new Bitmap(ButtonShowDesktopS);
            Bitmap ShowdesktopBmp = new Bitmap(ShowdesktopBmpTemp, ShowdesktopBmpTemp.Width, TaskPos.Height);
            ShowDesktop.SetBitmap(ShowdesktopBmp);
            ShowdesktopBmpTemp.Dispose();
            ShowdesktopBmp.Dispose();

        }

        void ShowDesktop_MouseHover(object sender, EventArgs e)
        {
            WindowsAPI.RECTW TaskPos = new WindowsAPI.RECTW();
            WindowsAPI.GetWindowRect(hReBar, ref TaskPos);
            Bitmap ShowdesktopBmpATemp = new Bitmap(ButtonShowDesktopAS);
            Bitmap ShowdesktopBmpA = new Bitmap(ShowdesktopBmpATemp, ShowdesktopBmpATemp.Width, TaskPos.Height);
            ShowDesktop.SetBitmap(ShowdesktopBmpA);
            ShowdesktopBmpATemp.Dispose();
            ShowdesktopBmpA.Dispose();
        }
          void SetValues()
        {
            try
            {
                WorkAreaReserve = Convert.ToBoolean(Convert.ToInt32(SettingsIni.IniReadValue("Main", "WorkAreaRes")));

            }
            catch
            {

            }
            try
            {
                ThuDisable = Convert.ToBoolean(Convert.ToInt32(SettingsIni.IniReadValue("Main", "DisableThu")));

            }
            catch
            {

            }
            try
            {
                Taskbarhide = Convert.ToBoolean(Convert.ToInt32(SettingsIni.IniReadValue("Main", "TaskbarHide")));
            }
            catch
            {
            }
            try
            {
                SkinDir = SettingsIni.IniReadValue("Main", "Skin");
            }
            catch
            {
                
            }
            if (SkinDir == "")
            {
                WindowsAPI.RECTW TaskHPos = new WindowsAPI.RECTW();
                WindowsAPI.GetWindowRect(hTask, ref TaskHPos);
                if (TaskHPos.Height > 30)
                {
                    SkinDir = "Aero"; 
                }
                else
                {
                    SkinDir = "Aero16";
                }
            }
            try
            {
                TaskXPos = Convert.ToInt32(SettingsIni.IniReadValue("Main", "XPos"));
            }
            catch
            {
            }
            try
            {
                TaskYPos = Convert.ToInt32(SettingsIni.IniReadValue("Main", "YPos"));
            }
            catch
            {

            }
            try
            {
                CountBadgeBool = Convert.ToInt32(SettingsIni.IniReadValue("Main", "Count"));
            }
            catch
            {
            }
            Skin = new IniFile(ExePath + "\\Skins\\" + SkinDir + "\\skin.ini");
            ButtonBmpS = ExePath + "\\Skins\\" + SkinDir + "\\6.png";
            ButtonBmpS2 = ExePath + "\\Skins\\" + SkinDir + "\\13.png";
            ButtonBmpS3 = ExePath + "\\Skins\\" + SkinDir + "\\9.png";
            ButtonPressedBmpS = ExePath + "\\Skins\\" + SkinDir + "\\7.png";
            ButtonPressedBmpS2 = ExePath + "\\Skins\\" + SkinDir + "\\15.png";
            ButtonPressedBmpS3 = ExePath + "\\Skins\\" + SkinDir + "\\14.png";
            ButtonHoverBmpS = ExePath + "\\Skins\\" + SkinDir + "\\3.png";
            ButtonHoverBmpS2 = ExePath + "\\Skins\\" + SkinDir + "\\5.png";
            ButtonHoverBmpS3 = ExePath + "\\Skins\\" + SkinDir + "\\4.png";
            ButtonQLH = ExePath + "\\Skins\\" + SkinDir + "\\2.png";
            ButtonFlash = new Bitmap(ExePath + "\\Skins\\" + SkinDir + "\\8.png");
            ButtonFlash2 = new Bitmap(ExePath + "\\Skins\\" + SkinDir + "\\17.png");
            ButtonFlash3 = new Bitmap(ExePath + "\\Skins\\" + SkinDir + "\\16.png"); 
            PopUpButtonBgr = new Bitmap(ExePath + "\\Skins\\" + SkinDir + "\\12.png");
            PopUpButtonBgrA = new Bitmap(ExePath + "\\Skins\\" + SkinDir + "\\11.png");
            PopUpButtonBgrAF = new Bitmap(ExePath + "\\Skins\\" + SkinDir + "\\11f.png");
            TransbgrS = ExePath + "\\Skins\\" + SkinDir + "\\2.png";
            HoverBgrThu = ExePath + "\\Skins\\" + SkinDir + "\\menu3.png";
            TransbgrAS = ExePath + "\\Skins\\" + SkinDir + "\\trs.png";
            CountBadgeS = ExePath + "\\Skins\\" + SkinDir + "\\18.png";
            FlyoutbgrWindowCloseS = ExePath + "\\Skins\\" + SkinDir + "\\22.png";
            TransS = ExePath + "\\Skins\\" + SkinDir + "\\30.png";
            FlyoutbgrS = ExePath + "\\Skins\\" + SkinDir + "\\20.png";
            Menu1S = ExePath + "\\Skins\\" + SkinDir + "\\44.png";
            FlashbgrS = ExePath + "\\Skins\\" + SkinDir + "\\menu2.png";
            ButtonShowDesktopS = ExePath + "\\Skins\\" + SkinDir + "\\ShowDesktop.png";
            ButtonShowDesktopAS = ExePath + "\\Skins\\" + SkinDir + "\\ShowDesktopA.png";
//            OperatingSystemVersion sv = new OperatingSystemVersion();
            if (sv.OSMajorVersion < 6)
            {
                icControlPanel = 21;
                icPrinterandfaxes = 58;
                icDocumentsandsettings = 0;
            }
            else
            {
                icControlPanel = 21;
                icPrinterandfaxes = 58;
            }
        }
          public static void RepositionSbar(int DevX, int DevY)
        {
            foreach (PerPixelAlphaForm F in Startbar.OwnedForms)
            {
                try
                {
                    F.OwnedForms[0].OwnedForms[0].Location = new Point(F.OwnedForms[0].OwnedForms[0].Location.X - DevX,
                                                                               F.OwnedForms[0].OwnedForms[0].Location.Y - DevY);
                }
                catch
                {

                }
                try
                {
                    F.OwnedForms[0].Location = new Point(F.OwnedForms[0].Location.X - DevX,
                                                                 F.OwnedForms[0].Location.Y - DevY);
                }
                catch
                {

                }
                try
                {                                                   
                    F.Location = new Point(F.Location.X - DevX, F.Location.Y - DevY);
                }
                catch
                {

                }

            }
        }

        void watcher_Changed(object sender, FileSystemEventArgs e)
        {
            RecDoc.Clear();
            listView1.Items.Clear();
            System.IO.DirectoryInfo Recdir =
                new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Recent));
            foreach (FileInfo file in Recdir.GetFiles())
            {
                ListViewItem lv = new ListViewItem(file.FullName);

                lv.SubItems.Add(File.GetCreationTimeUtc(file.FullName).ToFileTimeUtc().ToString());
                listView1.Items.Add(lv);
                //                RecDoc.Add(file.FullName);
            }
            foreach (ListViewItem lvi in listView1.Items)
            {
                RecDoc.Add(lvi.Text);
            }
        }

        void watcher_Created(object sender, FileSystemEventArgs e)
        {
            RecDoc.Clear();
            listView1.Items.Clear();
            System.IO.DirectoryInfo Recdir =
                new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Recent));
            foreach (FileInfo file in Recdir.GetFiles())
            {
                ListViewItem lv = new ListViewItem(file.FullName);
                lv.SubItems.Add(File.GetCreationTimeUtc(file.FullName).ToFileTimeUtc().ToString());
                listView1.Items.Add(lv);
                //                RecDoc.Add(file.FullName);
            }
            foreach (ListViewItem lvi in listView1.Items)
            {
                RecDoc.Add(lvi.Text);
            }
        }

        void Flash_Window(IntPtr Handle)
        {
            string s = "";
            try
            {
                s = Path.GetFileName(WindowsAPI.GetWindowModuleFileName(Handle)).ToLower();
            }
            catch
            {


            }

            try
            {
                string fw = Foreground.ToString(); //WindowsAPI.GetForegroundWindow().ToString();
                string fww = WindowsAPI.GetForegroundWindow().ToString();
                var F = FromHandle((IntPtr)Convert.ToInt32(treeListView1.Nodes[treeListView1.Nodes.IndexOf(treeListView1.GetItemAt(s))].SubItems[0].Text)) as PerPixelAlphaForm;
                try
                {
                    if (F != null)
                    {
                        for (int i = 0; i < treeListView1.Nodes[treeListView1.Nodes.IndexOf(treeListView1.GetItemAt(s))].Nodes.Count; i++)
                        {
                            if (treeListView1.Nodes[treeListView1.Nodes.IndexOf(treeListView1.GetItemAt(s))].Nodes[i].SubItems[2].Text == Handle.ToString())
                            {
                                treeListView1.Nodes[treeListView1.Nodes.IndexOf(treeListView1.GetItemAt(s))].Nodes[i].
                                    SubItems[7].Text = "Flashing";
                            }
                        }
                        TreeListNode TTT = null;
                        for (int i = 0; i < treeListView1.Nodes.Count; i++)
                        {
                            if (treeListView1.Nodes[i].SubItems[0].Text == F.Handle.ToString())
                            {
                                TTT = treeListView1.Nodes[i];
                            }

                        }

                        Bitmap FlashBMP = new Bitmap(ButtonFlash);
                        if (TTT.Nodes.Count == 2)
                        {
                            F.SetBitmap(ButtonFlash2);
                        }
                        else if (TTT.Nodes.Count > 2)
                        {
                            F.SetBitmap(ButtonFlash3);
                        }
                        else
                        {
                            F.SetBitmap(FlashBMP);
                        }
//                        F.SetBitmap(FlashBMP);
                        F.Tag = "Flashing";
                        FlashBMP.Dispose();
                        FlashBMP = null;
                        GC.Collect();
                        GC.WaitForPendingFinalizers();
                        GC.Collect();
                        //                    FlushMemory();
                    }
                    else
                    {
                        F.Tag = "";
                    }
                }
                catch
                {


                }
            }
            catch
            {

            }
        }

	    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {

            // performance. And it's just plain sloppy.

//            if (Taskbarhide)
//            {
                WindowsAPI.ShowWindow((int)hTask, (int)WindowsAPI.SW.SHOW); 
//            }

            var rrr = new WindowsAPI.RECT();
            rrr.Left = Desktop.Left;
            rrr.Right = Desktop.Right;
            rrr.Top = Desktop.Top;
            rrr.Bottom = Desktop.Bottom + 0;
            IntPtr p = Marshal.AllocHGlobal(Marshal.SizeOf(rrr));
            Marshal.StructureToPtr(rrr, p, true);
            if (sv.OSMajorVersion != 6)
            {
                var di = new DirectoryInfo(ExePath + "\\Thumb\\");
                System.IO.FileInfo[] fi = di.GetFiles();
                foreach (FileInfo info in fi)
                {
                    info.Delete();
                } 
            }
            timer1.Stop();
            if (WorkAreaReserve)
            {
                WindowsAPI.SystemParametersInfo(WindowsAPI.SPI.SPI_SETWORKAREA, (uint)Marshal.SizeOf(rrr), p, WindowsAPI.SPIF.SPIF_SENDCHANGE);
                timer1.Start();
                
            }
        }
        public   void DiableTimer ()
        {
            
        }
        public   void EnableTimer()
        {
            
        }

	    private void button1_Click(object sender, EventArgs e)
        {
            
	        WindowsAPI.ShowWindow((int) Handle, (int) WindowsAPI.SW.HIDE);
        }

	    private void button3_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            var iniDesktop = new WindowsAPI.RECT();
            IntPtr ptr = IntPtr.Zero;

            ptr = Marshal.AllocHGlobal(Marshal.SizeOf(iniDesktop));

            Marshal.StructureToPtr(iniDesktop, ptr, true);

            WindowsAPI.SystemParametersInfo(WindowsAPI.SPI.SPI_GETWORKAREA, (uint)Marshal.SizeOf(iniDesktop), ptr, 0);

            iniDesktop = (WindowsAPI.RECT)Marshal.PtrToStructure(ptr, new WindowsAPI.RECT().GetType());

            if (iniDesktop.Bottom != InitialAreaY)
            {
                var rrr = new WindowsAPI.RECT();
                rrr.Left = 0;
                rrr.Right = 0;
                rrr.Top = InitialareaX;
                rrr.Bottom = InitialAreaY;
                IntPtr p = Marshal.AllocHGlobal(Marshal.SizeOf(rrr));
                Marshal.StructureToPtr(rrr, p, true);
                WindowsAPI.SystemParametersInfo(WindowsAPI.SPI.SPI_SETWORKAREA, (uint)Marshal.SizeOf(rrr), p, WindowsAPI.SPIF.SPIF_SENDCHANGE); 
            }

        }

        private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var Opt = new Options();
            Opt.ShowDialog();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void developperToolStripMenuItem_Click(object sender, EventArgs e)
        {

            WindowsAPI.ShowWindow((int)Handle, (int)WindowsAPI.SW.RESTORE);
            WindowsAPI.SetForegroundWindow(Handle);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            foreach (Form window in Startbar.OwnedForms)
            {
                window.TopMost = true;
            }
            Startbar.TopMost = true;
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
//            OwnedForms[0].TopMost = true;
//            WindowsAPI.SetWindowPos(OwnedForms[0].Handle, WindowsAPI.HWND_TOPMOST, 0,
//                                    0, 0, 0,
//                                    WindowsAPI.SWP_NOMOVE | WindowsAPI.SWP_NOSIZE | WindowsAPI.SWP_NOREDRAW );

//            WindowsAPI.SetWindowPos(hTray, WindowsAPI.HWND_BOTTOM, 0,
//                                    0, 0, 0,
//                                    WindowsAPI.SWP_NOMOVE | WindowsAPI.SWP_NOSIZE | WindowsAPI.SWP_NOREDRAW |WindowsAPI.SWP_NOSENDCHANGING |WindowsAPI.SWP_NOACTIVATE);

//            (sender as Timer).Stop();
        }

	    private void FlushMemmory_Tick(object sender, EventArgs e)
        {
            FlushMemory();
        }
        public static void ShowHideTaskbar(bool B)
        {
            if (B)
            {
              WindowsAPI.ShowWindow((int)hTask, (int)WindowsAPI.SW.SHOW);  
            }
            else
            {
                WindowsAPI.ShowWindow((int)hTask, (int)WindowsAPI.SW.HIDE);
            }
        }

        public static bool IsWindowTopMost(IntPtr Handle)
        {
            return (WindowsAPI.GetWindowLong(Handle, WindowsAPI.GWL_EXSTYLE) & WindowsAPI.WS_EX_TOPMOST) != 0;
        }
        public   bool GetScreenSaverRunning()
        {
            bool isRunning = false;

            WindowsAPI.SystemParametersInfo((int) WindowsAPI.SPI.SPI_GETSCREENSAVERRUNNING, 0,
               ref isRunning, 0);
            return isRunning;
        }

        private void timer2_Tick_1(object sender, EventArgs e)
        {
            WindowsAPI.RECTW r = new WindowsAPI.RECTW();
            WindowsAPI.GetWindowRect(WindowsAPI.GetForegroundWindow(), ref r);
            if ((r.Width > Screen.PrimaryScreen.WorkingArea.Width - 1 & r.Height > Screen.PrimaryScreen.WorkingArea.Height) & IsWindowTopMost(WindowsAPI.GetForegroundWindow()))
            {
//                timer3.Start();
                FullScreenApp = WindowsAPI.GetForegroundWindow();
            }
            else
            {
//                timer3.Stop();
                FullScreenApp = IntPtr.Zero;
            }
        }
        public void GenerateThumb(IntPtr WinHandle)
        {
            float XLenB = 0;
            float YLenB = 0;
            int ScaleW = Desktop.Top / Desktop.Bottom;
            int MaxSize = Convert.ToInt32(SettingsIni.IniReadValue("Main", "TrackBarThu"));
            var REC = new WindowsAPI.RECTW();
            WindowsAPI.GetWindowRect(WinHandle, ref REC);
            float We = REC.Width;
            float He = REC.Height;
            float Ratio = We/He;
            if (REC.Width > REC.Height)
            {
                XLenB = MaxSize;
                YLenB = XLenB / Ratio;
            }
            else if (REC.Width < REC.Height)
            {
                YLenB = MaxSize;
                XLenB = MaxSize * Ratio;
            }
            else
            {
                YLenB = MaxSize;
                XLenB = MaxSize;
            }
//            Bitmap TransSB = new Bitmap(TransS);
            var WindowB = new Bitmap(MaxSize, (MaxSize /ScaleW));
            WindowB.MakeTransparent();
//            TransSB.Dispose();
//            TransSB = null;
            Bitmap Temp;
            int YLenBDivider = 0;
            if (YLenB >= MaxSize - 27)
            {
                YLenBDivider = 22;
            }
            else
            {
                YLenBDivider = 0;
            }
            IntPtr SDC = Win32API.GetDC(WinHandle);
            IntPtr bmDC = Win32API.CreateCompatibleDC(SDC);
            IntPtr TempHP = WindowsAPI.CreateCompatibleBitmap(SDC, REC.Width, REC.Height);
            WindowsAPI.SelectObject(bmDC, TempHP);
            WindowsAPI.PrintWindow(WinHandle, bmDC, 0);
            Win32API.ReleaseDC(WinHandle, SDC);
            Win32API.DeleteDC(bmDC);
            Temp = Image.FromHbitmap(TempHP);

            Graphics g = Graphics.FromImage(WindowB);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
            g.DrawImage(Temp, (MaxSize - XLenB) / 2, (MaxSize - YLenB) / 2 + YLenBDivider, XLenB,
                                YLenB - YLenBDivider);
            Icon I =
                lsts.Icon(
                    lsts.IconIndex(
                        WindowsAPI.GetWindowModuleFileName(WinHandle)));
            g.DrawImage(I.ToBitmap(), 5, 5);
            g.DrawString(
                WindowsAPI.GetWindowName(WinHandle),
                new Font("Calibri", 10), Brushes.Black, new PointF(24, 5));
            I.Dispose();
            I = null;
            WindowB.Save(ExePath + "\\Thumb\\" + WinHandle.ToString() + ".png");
        }

        private void timer3_Tick(object sender, EventArgs e)
        {

//            if (FullScreenApp == IntPtr.Zero)
//            {
//                foreach (Form window in Startbar.OwnedForms)
//                {
//                    window.TopMost = true;
//                    window.BringToFront();
//                } 
//            }
//            while (!IsWindowTopMost(Startbar.Handle) & FullScreenApp == IntPtr.Zero)
//            {
//               timer3.Start(); 
//            }
//            timer3.Stop();


        }

        private void timer4_Tick(object sender, EventArgs e)
        {
            if (!File.Exists(ExePath + "\\Thumb\\" + (sender as Timer).Tag.ToString() + ".png"))
            {
                GenerateThumb((IntPtr)(sender as Timer).Tag); 
            }
            timer4.Stop();
        }

        private void timer5_Tick(object sender, EventArgs e)
        {

            try
            {
                if (Pop2 != null)
                {
                    Form TempForm = FromHandle((IntPtr)Pop2.Tag) as Form;
                    Rectangle Button = new Rectangle(TempForm.Location.X, TempForm.Location.Y, TempForm.Width, TempForm.Height);
                    Rectangle Pop = new Rectangle(Pop2.Location.X, Pop2.Location.Y, Pop2.Width, Pop2.Height);
                    if (!Button.Contains(MousePosition) & !Pop.Contains(MousePosition))
                    {
                        Pop2.Dispose();
                    }

                }
                if (RightClickForm != null)
                {
                    Form TempForm1 = (PForm.Tag) as Form;
                    Rectangle Button1 = new Rectangle(TempForm1.Location.X, TempForm1.Location.Y, TempForm1.Width, TempForm1.Height);
                    Rectangle Pop1 = new Rectangle(RightClickForm.Location.X, RightClickForm.Location.Y, RightClickForm.Width, RightClickForm.Height);
                    if (!Button1.Contains(MousePosition) & !Pop1.Contains(MousePosition))
                    {
                        RightClickForm.Dispose();
                    }

                }
            }
            catch
            {
                

            }
        }

        private void UpdateTimer_Tick(object sender, EventArgs e)
        {
            if (File.Exists(Path.GetDirectoryName(Application.ExecutablePath) + @"\WebUpdate.exe"))
            {
                Process.Start(new ProcessStartInfo(
                                  Path.GetDirectoryName(Application.ExecutablePath) + @"\WebUpdate.exe",
                                  "1"));
            }
        }

        private void checkForUpdateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (File.Exists(Path.GetDirectoryName(Application.ExecutablePath) + @"\WebUpdate.exe"))
            {
                Process.Start(new ProcessStartInfo(Path.GetDirectoryName(Application.ExecutablePath) + @"\WebUpdate.exe",
                                                       "0")); 
            }
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox About = new AboutBox();
            About.ShowDialog();
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            timer3.Stop();
        }

        private void contextMenuStrip1_Opened(object sender, EventArgs e)
        {
            if (File.Exists(Path.GetDirectoryName(Application.ExecutablePath) + @"\WebUpdate.exe"))
            {
                miUpdateCheck.Enabled = true;
            }
            else
            {
                miUpdateCheck.Enabled = false;
            }
        }

        private void frmSbarDevelopper_LocationChanged(object sender, EventArgs e)
        {

        }

        private void trPop_Tick(object sender, EventArgs e)
        {
            if (Pop != null)
            {
                Form TempForm = FromHandle((IntPtr)(sender as Timer).Tag) as Form;
                Rectangle Button = new Rectangle(TempForm.Location.X, TempForm.Location.Y, TempForm.Width, TempForm.Height);
                Rectangle PopR = new Rectangle(Pop.Location.X, Pop.Location.Y, Pop.Width, Pop.Height);
                if (!Button.Contains(MousePosition) & !PopR.Contains(MousePosition))
                {
                    Pop.Dispose();
                }

            }
        }

        private void tmDrop_Tick(object sender, EventArgs e)
        {
            int AllighYL = 0;
            try
            {
                AllighYL = Convert.ToInt32(Skin.IniReadValue("TASKBARLEFTCLICK", "YAlligment"));
            }
            catch
            {

                MessageBox.Show("Invalid Skin");

            }

            TreeListNode TTT = (sender as Timer).Tag as TreeListNode;
            if (TTT.GetNodeCount(false) <= 1)
            {
                var winh = (IntPtr)Convert.ToInt32(TTT.Nodes[0].SubItems[2].Text);
                if ((WindowsAPI.GetWindowLong(winh, WindowsAPI.GWL_STYLE) & WindowsAPI.WS_MINIMIZE) != 0)
                {
                    WindowsAPI.SetForegroundWindow(winh);
                    Foreground = winh;
                    WindowsAPI.ShowWindow((int)winh, (int)WindowsAPI.SW.RESTORE);
                }
                else
                {
                    WindowsAPI.SetForegroundWindow(winh);
                    Foreground = winh;
                }

            }
            else
            {
                LoadLeftClickMenu((FromHandle((IntPtr)Convert.ToInt32(TTT.SubItems[0].Text)) as PerPixelAlphaForm), AllighYL);
            }
            tmDrop.Stop();
        }

        private void tmDropP_Tick(object sender, EventArgs e)
        {
            var winh = (IntPtr)Convert.ToInt32((sender as Timer).Tag);
            if ((WindowsAPI.GetWindowLong(winh, WindowsAPI.GWL_STYLE) & WindowsAPI.WS_MINIMIZE) != 0)
            {
                WindowsAPI.SetForegroundWindow(winh);
                Foreground = winh;
                WindowsAPI.ShowWindow((int)winh, (int)WindowsAPI.SW.RESTORE);
            }
            else
            {
                WindowsAPI.SetForegroundWindow(winh);
                Foreground = winh;
            }
            tmDropP.Stop();
        }


        private void tmAutoHide_Tick(object sender, EventArgs e)
        {
            WindowsAPI.RECTW TaskRPos = new WindowsAPI.RECTW();
            WindowsAPI.GetWindowRect(hReBar, ref TaskRPos);
//            if (TaskRPos.Top != P.Y)
//            {
//                RepositionSbar(0, P.Y - TaskRPos.Top); 
//            }
            if (TaskRPos.Top != TaskbarPosY)
            {
                RepositionSbar(0, TaskbarPosY -TaskRPos.Top);
                TaskbarPosY = TaskRPos.Top;
            }
        }

        private void frmSbarDevelopper_Shown(object sender, EventArgs e)
        {
            TLV = treeListView1;
            
        }

        private void tmrClean_Tick(object sender, EventArgs e)
        {
            CleanT = new Thread(new ThreadStart(CleanTaskbar));
            CleanT.Start();
        }


        private void menuItem4_Click(object sender, EventArgs e)
        {
            WindowsAPI.PostMessage(hTray, (uint)WindowsAPI.WndMsg.WM_COMMAND, (IntPtr)WindowsAPI.TaskbarMessageParams.LockTaskbar, IntPtr.Zero);
            if ((sender as MenuItem).Checked)
            {
                (sender as MenuItem).Checked = false; 
            }
            else
            {
                (sender as MenuItem).Checked = true;
            }
        }

        private void menuItem5_Click(object sender, EventArgs e)
        {
//            Process.Start("rundll32.exe", "shell32.dll,Options_RunDLL 1");
            WindowsAPI.PostMessage(hTray, (uint)WindowsAPI.WndMsg.WM_COMMAND, (IntPtr)WindowsAPI.TaskbarMessageParams.ShowTaskbarPrps, IntPtr.Zero);

        }

        private void menuItem6_Click(object sender, EventArgs e)
        {
            WindowsAPI.Shell shell = new WindowsAPI.Shell();
            if (IsPushMinimize)
            {
                shell.UndoMinimizeALL();
                IsPushMinimize = false;
            }
            else
            {
                shell.MinimizeAll();
                IsPushMinimize = true;
            }
        }

        private void miCascade_Click(object sender, EventArgs e)
        {
            WindowsAPI.PostMessage(hTray, (uint)WindowsAPI.WndMsg.WM_COMMAND, (IntPtr)WindowsAPI.TaskbarMessageParams.ArrangeCascade, IntPtr.Zero);
        }

        private void miSidebySideWin_Click(object sender, EventArgs e)
        {
            WindowsAPI.PostMessage(hTray, (uint)WindowsAPI.WndMsg.WM_COMMAND, (IntPtr)WindowsAPI.TaskbarMessageParams.ArrangeTileHrz, IntPtr.Zero);
        }

        private void miStackedWin_Click(object sender, EventArgs e)
        {
            WindowsAPI.PostMessage(hTray, (uint)WindowsAPI.WndMsg.WM_COMMAND, (IntPtr)WindowsAPI.TaskbarMessageParams.ArrangeTileVrt, IntPtr.Zero);
        }

        private void miTaskmanager_Click(object sender, EventArgs e)
        {
//            Process.Start("taskmgr");
            WindowsAPI.PostMessage(hTray, (uint)WindowsAPI.WndMsg.WM_COMMAND, (IntPtr)WindowsAPI.TaskbarMessageParams.ShowTaskMngr, IntPtr.Zero);
        }

        private void tmRefreshDropZone_Tick(object sender, EventArgs e)
        {
            WindowsAPI.RECTW TaskHPos = new WindowsAPI.RECTW();
            WindowsAPI.GetWindowRect(hTask, ref TaskHPos);
            if (TaskHPos.Width != TaskWidth | TaskHPos.Height != TaskHeight)
            {
                Bitmap GHJ = new Bitmap(StartBarBackgroubd,TaskHPos.Width,TaskHPos.Height); 
                F1.SetBitmap(GHJ,25);
                GHJ.Dispose();
                TaskWidth = TaskHPos.Width;
                TaskHeight = TaskHPos.Height;
            }
        }

        private void tmFullScreenDetect_Tick(object sender, EventArgs e)
        {
            WindowsAPI.RECTW WindowRect = new WindowsAPI.RECTW();
            WindowsAPI.GetWindowRect(WindowsAPI.GetForegroundWindow(), ref WindowRect);

            string PathI = "";
            try
            {
                PathI =
                        Path.GetFileName(WindowsAPI.GetWindowModuleFileName(WindowsAPI.GetForegroundWindow())).ToLowerInvariant();
            }
            catch
            {
                PathI = "";
            }
            if (PathI != "explorer.exe" & PathI != "" & (WindowsAPI.GetWindowLongA(WindowsAPI.GetForegroundWindow(), WindowsAPI.GWL_STYLE) & WindowsAPI.WS_BORDER) != WindowsAPI.WS_BORDER & Screen.PrimaryScreen.Bounds.Width <= WindowRect.Width & Screen.PrimaryScreen.Bounds.Height <= WindowRect.Height)
            {
                FullScreenApp = Foreground;
                foreach (Form FF in Startbar.OwnedForms)
                {
                    WindowsAPI.ShowWindow((int)FF.Handle, 0);
                    try
                    {
                        WindowsAPI.ShowWindow((int)FF.OwnedForms[0].Handle, 0);
                    }
                    catch
                    {

                    }

                }
            }
            else
            {
                FullScreenApp = IntPtr.Zero;
                foreach (Form FF in Startbar.OwnedForms)
                {
                    WindowsAPI.ShowWindow((int)FF.Handle, 1);
                    try
                    {
                        WindowsAPI.ShowWindow((int)FF.OwnedForms[0].Handle, 1);
                    }
                    catch
                    {

                    }

                }
            }
        }
	}
}
